且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

问题发送电子邮件

更新时间:2023-02-25 11:16:33

您execption应包含比发送邮件失败的详细信息。

有关这个链接调试找对方法SmtpClient.Send抛出产生的异常详细信息 - >的 SmtpClient.Send方法

这code作品有smtp.google.com

 毫米MAILMESSAGE新= MAILMESSAGE();
        mm.From =新的MailAddress(XX);
        mm.To.Add(XX);
        mm.Subject =蚂蚁主题;
        mm.Body =身体茶MEssag这里;        SmtpClient SS =新SmtpClient();
        ss.Host =smtp.gmail.com;
        ss.Port = 587;
        ss.EnableSsl = TRUE;        ss.Credentials =新System.Net.NetworkCredential(XX,XX);
        尝试
        {
            ss.Send(毫米);
            Label1.Text =消息发送;
            Label1.ForeColor = System.Drawing.Color.Green;
        }
        赶上(SmtpException前)
        {
            Label1.Text =消息发送失败:\\ n \\ n+ ex.Message;
            Label1.ForeColor = System.Drawing.Color.Red;
        }

谷歌需要启用SSL和端口587是传出端口。你需要一个谷歌帐户,以及凭据。

没有错,你的code - 这是最有可能您的服务器或防火墙

The send method of SMTP is always throwing an exception. The message is : Failure sending mail

Here is my code:

MailMessage mm = new MailMessage();
mm.To.Add("Yuvraj.dhot@xxxxx.co.in");

mm.From = new MailAddress("Kumar.Chaudhari@xxxxx.co.in");
mm.Subject = "Ant Subject";
mm.Body = "Body Cha MEssag here ";

SmtpClient ss = new SmtpClient("localhost");

ss.EnableSsl = false;

try
{
    **ss.Send(mm);**
    Result.Text = "Message Sent";
    Result.ForeColor = System.Drawing.Color.Green;
}
catch (SmtpException ex)
{
    Result.Text = "Message Not Sent : \n\n " + ex.Message;
    Result.ForeColor = System.Drawing.Color.Red;
}

I also tried using

ss.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;

Now it doesn't throw any exception, it executes fine but receiver does not receive any mail in his inbox.

How can I fix this?

Edit - This is the stack trace im getting

    Message=Failure sending mail. Source=System StackTrace: at System.Net.Mail.SmtpClient.Send
(MailMessage message) at WebApplication1._Default.Page_Load(Object sender, EventArgs e) in D:\Emcure-
Kumar\Work\Not in Use\WebApplication1\WebApplication1\Default.aspx.cs:line 30 InnerException: 
System.Net.WebException Message=Unable to connect to the remote server Source=System StackTrace: –
    at System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object owner, Boolean async,
 IPAddress& address, Socket& abortSocket, Socket& abortSocket6, Int32 timeout) at 
System.Net.PooledStream.Activate(Object owningObject, Boolean async, Int32 timeout, 
GeneralAsyncDelegate asyncCallback) at System.Net.PooledStream.Activate(Object owningObject, 
GeneralAsyncDelegate asyncCallback) at System.Net.ConnectionPool.GetConnection(Object owningObject, 
GeneralAsyncDelegate asyncCallback, Int32 creationTimeout)
    at System.Net.Mail.SmtpConnection.GetConnection(String host, Int32 port) at 
System.Net.Mail.SmtpTransport.GetConnection(String host, Int32 port) at 
System.Net.Mail.SmtpClient.GetConnection() at System.Net.Mail.SmtpClient.Send(MailMessage message) 
InnerException: System.Net.Sockets.SocketException Message=No connection could be made because the 
target machine actively refused it 127.0.0.1:25 Source=System ErrorCode=10061 NativeErrorCode=10061

Your execption should contain more info than "Failure sending mail"

For debugging look at this link for details on the Exeptions thrown by SmtpClient.Send Method - > SmtpClient.Send Method

This code works with "smtp.google.com"

        MailMessage mm = new MailMessage();
        mm.From = new MailAddress("xx");
        mm.To.Add("xx");
        mm.Subject = "Ant Subject"; 
        mm.Body = "Body Cha MEssag here ";

        SmtpClient ss = new SmtpClient();
        ss.Host="smtp.gmail.com";
        ss.Port = 587;
        ss.EnableSsl = true;

        ss.Credentials = new System.Net.NetworkCredential("xx", "xx");
        try 
        { 
            ss.Send(mm); 
            Label1.Text = "Message Sent";
            Label1.ForeColor = System.Drawing.Color.Green; 
        }
        catch (SmtpException ex) 
        { 
            Label1.Text = "Message Not Sent : \n\n " + ex.Message;
            Label1.ForeColor = System.Drawing.Color.Red;
        }

Google requires SSL enabled and port 587 to be the outgoing port. You need a google account as well for credentials.

Nothing wrong with your code - it's most likely your server or firewall