tags:

views:

94

answers:

3

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!!

+2  A: 

Something like this:

System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage("[email protected]", "[email protected]", "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);
Carko
+2  A: 

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
    }
Rodrick Chapman
A: 

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

    }
}
Laramie