通过 C# 发送 Gmail [重复]

Posted

技术标签:

【中文标题】通过 C# 发送 Gmail [重复]【英文标题】:Sending Gmail through C# [duplicate] 【发布时间】:2011-03-08 23:11:43 【问题描述】:

可能重复:Sending Email in .NET Through Gmail

我在通过 C# 发送邮件时遇到很多问题。我已经在多个应用程序上尝试过,但它永远无法正常工作....

有人可以发布一些示例代码,清楚地标明发件人和收件人的去向,并提供有关 smtp sever dat 或其他什么的帮助!

【问题讨论】:

见Sending Email in .NET Through Gmail。如果您解释“它永远不会起作用”会有所帮助 【参考方案1】:

类似这样的:

System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage("sender@gmail.com", "recipient@example.com", "subject", "body");
System.Net.Mail.SmtpClient emailClient = new System.Net.Mail.SmtpClient("smtp.gmail.com", 465);
emailClient.Credentials = new System.Net.NetworkCredential("yourgmailusername", "yourpassword");
emailClient.Send(message);

【讨论】:

【参考方案2】:

我前段时间编写的一些用于通过网络表单发送电子邮件的代码:

    //using System.Net.Mail;

    MailMessage msg = new MailMessage();

    msg.To.Add(RECIPIENT_ADDRESS); //note that you can add arbitrarily many recipient addresses


    msg.From = new MailAddress(SENDER_ADDRESS, RECIPIENT_NAME, System.Text.Encoding.UTF8);
    msg.Subject = SUBJECT
    msg.SubjectEncoding = System.Text.Encoding.UTF8;
    msg.Body = //SOME String

    msg.BodyEncoding = System.Text.Encoding.UTF8;
    msg.IsBodyhtml = false;

    SmtpClient client = new SmtpClient();     

    client.Credentials = new System.Net.NetworkCredential(ADDRESS, PASSWORD);
    client.Port = 587;
    client.Host = "smtp.gmail.com";
    client.EnableSsl = true;

    try
    
        client.Send(msg);
    
    catch (SmtpException ex)
    
        throw; //or handle here
    

【讨论】:

【参考方案3】:

我让这个类在我的开发环境中通过我的 gmail 帐户发送,并在生产时在我的 Web.Config 中使用 SMTP。与 noblethrasher 基本相同,但部署舒适。

“mailConfigTest”有一个标志

/// <summary>
/// Send Mail to using gmail in test, SMTP in production
/// </summary>
public class MailGen

    bool _isTest = false;
    public MailGen()
    
        _isTest = (WebConfigurationManager.AppSettings["mailConfigTest"] == "true");
    

    public void SendMessage(string toAddy, string fromAddy, string subject, string body)
    
        string gmailUser = WebConfigurationManager.AppSettings["gmailUser"];
        string gmailPass = WebConfigurationManager.AppSettings["gmailPass"];
        string gmailAddy = WebConfigurationManager.AppSettings["gmailAddy"];


        NetworkCredential loginInfo = new NetworkCredential(gmailUser, gmailPass);
        MailMessage msg = new MailMessage();
        SmtpClient client = null;

        if (_isTest) fromAddy = gmailAddy;

        msg.From = new MailAddress(fromAddy);
        msg.To.Add(new MailAddress(toAddy));
        msg.Subject = subject;
        msg.Body = body;
        msg.IsBodyHtml = true;
        if (_isTest)
        
            client = new SmtpClient("smtp.gmail.com", 587);
            client.EnableSsl = true;
            client.UseDefaultCredentials = false;
            client.Credentials = loginInfo;
        
        else
        
            client = new SmtpClient(WebConfigurationManager.AppSettings["smtpServer"]);
        
        client.DeliveryMethod = SmtpDeliveryMethod.Network;
        client.Send(msg);

    

【讨论】:

以上是关于通过 C# 发送 Gmail [重复]的主要内容,如果未能解决你的问题,请参考以下文章

.Net通过Gmail发送邮件[重复]

通过 smtp(gmail 服务器)发送邮件 [重复]

通过 PHP 从免费的 Gmail 帐户发送 [重复]

从c#代码发送电子邮件[重复]

wamp sendmail 无法通过 smtp.gmail.com:587 发送电子邮件 [重复]

无法通过 gmail 从新创建的电子邮件中使用 phpMailer 发送 SMTP 邮件 [重复]