开发者

Sending Gmail through C# [duplicate]

This question already has answers here: Closed 12 years ago.

Possible Duplicate:

Sending Email in .NET Through Gmail

I have so many problems with sending mail through C#. I have tried forever on multiple apps and it never works..开发者_高级运维..

Could someone PLEASE post some sample code that clearly labels where the sender and recipient go and offers help with the smtp sever dat or whatever!!


Something like this:

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);


Some code that I wrote some time ago for sending email through a webform:

    //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
    }


I made this class to send via my gmail account when in my dev environment and use the SMTP in my Web.Config when in production. Essentially the same as noblethrasher with some deployment comfort.

There is a flag for "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);

    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜