开发者

How sent e-mail from ASP.NET site using GMail's SMTP server? [duplicate]

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

Possible Duplicate:

Sending email through Gmail SMTP server with C#

I would like my ASP.NET MVC application to send some standard letters to users of the web site. For testing purposes I have no local SMTP server and my provider has non that I know of. So I have to use public services like GMail's SMTP.

How do I send e-mail using smtp.gmail.com and my GMail account? What exactly should I put to Web.config and whot to code provided my e-mail is drastomail@gmail.com and my password is password?

Thank you


EDIT

When I try following 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();
    }
}

It fails with exception. Most inner exception carries this message:

No connection could be made because the target machine 开发者_运维百科actively refused it 209.85.227.109:587

Also any all present answers (3 earliest) give me the same exception. What can I do with that?


Here's a class I've used in the past:

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

The class needs to be configured in your Application_Start event:

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

Usage:

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


This could be a firewall issue. The firewall on the server your code is running on could be blocking traffic on TCP port 587. It is also possible that it is being blocked in the network infrastructure between your server and the Internet.


Use the simple utility class as below:

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜