How is email sent from an ASP.NET site using the GMail SMTP server?

Possible duplicate:
Sending email through a Gmail SMTP server using C #

I would like my ASP.NET MVC application to send standard emails to site users. For testing purposes, I do not have a local SMTP server, and my provider does not have the one I know about. Therefore, I must use public services such as GMail SMTP.

How do I send an email using smtp.gmail.commy GMail account? What exactly should I put in Web.config, and for the code, provided that my email address drastomail@gmail.comand my password are password?

thank


EDIT

When I try to run a demo program:

class Program {
    static void Main(string[] args) {
        var client = new SmtpClient("smtp.gmail.com", 587) {
            Credentials = new NetworkCredential("puzzlehunters@gmail.com", "puzzlehunters111"),
            EnableSsl = true
        };
        client.Send("puzzlehunters@gmail.com", "puzzlehunters@gmail.com", "test", "testbody");
        Console.WriteLine("Sent");
        Console.ReadLine();
    }
}

. :

No connection could be made because the target machine actively refused it 209.85.227.109:587

(3 ) . ?

+3
3

. , , TCP- 587. , .

+2

, :

namespace MyApp
{
    public class GMailer
    {
        public static string GmailUsername { get; set; }
        public static string GmailPassword { get; set; }
        public static string GmailHost { get; set; }
        public static int GmailPort { get; set; }
        public static bool GmailSSL { get; set; }

        public string ToEmail { get; set; }
        public string Subject { get; set; }
        public string Body { get; set; }
        public bool IsHtml { get; set; }

        static GMailer()
        {
            GmailHost = "smtp.gmail.com";
            GmailPort = 25; // Gmail can use ports 25, 465 & 587; but must be 25 for medium trust environment.
            GmailSSL = true;
        }

        public void Send()
        {
            SmtpClient smtp = new SmtpClient();
            smtp.Host = GmailHost;
            smtp.Port = GmailPort;
            smtp.EnableSsl = GmailSSL;
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtp.UseDefaultCredentials = false;
            smtp.Credentials = new NetworkCredential(GmailUsername, GmailPassword);

            using (var message = new MailMessage(GmailUsername, ToEmail))
            {
                message.Subject = Subject;
                message.Body = Body;
                message.IsBodyHtml = IsHtml;
                smtp.Send(message);
            }
        }
    }
}

Application_Start:

GMailer.GmailUsername = "you@gmail.com";
GMailer.GmailPassword = "password";

:

GMailer mailer = new GMailer();
mailer.ToEmail = "someone@somewhere.com";
mailer.Subject = "Email Subject Line";
mailer.Body = "This is a test message";
mailer.IsHtml = false;
mailer.Send();
+3

, :

using System.IO;
using System.Net.Mail;
using System.Text;
using System.Net;
public sealed class Emailer
{
    private Emailer()
    {
    }

    public static void SendMail(string subject, string to, 
        string from = null, string body = null, Stream attachment = null,
        int port = 25, string host = "localhost", bool isBodyHtml = true)
    {
        MailMessage mailMsg = new MailMessage();
        mailMsg.From = new MailAddress(from);
        mailMsg.To.Add(to);
        mailMsg.Subject = subject;
        mailMsg.IsBodyHtml = isBodyHtml;
        mailMsg.BodyEncoding = Encoding.UTF8;
        mailMsg.Body = body;
        mailMsg.Priority = MailPriority.Normal;

        //Message attahment
        if (attachment != null)
            mailMsg.Attachments.Add(new Attachment(attachment, "my.text"));

        // Smtp configuration
        SmtpClient client = new SmtpClient();
        client.Credentials = new NetworkCredential("YOUR_GMAIL_USER_NAME", "PASSWORD");
        client.UseDefaultCredentials = true;
        client.DeliveryMethod = SmtpDeliveryMethod.Network;
        client.Port = port; //use 465 or 587 for gmail           
        client.Host = host;//for gmail "smtp.gmail.com";
        client.EnableSsl = false;

        MailMessage message = mailMsg;

        client.Send(message);

    }

}
0

All Articles