使用smtp服务器,asp.net发送电子邮件失败

时间:2015-04-17 06:49:03

标签: c# asp.net smtp

我正在尝试通过asp.net C#代码发送电子邮件。但此代码不发送电子邮件并生成错误,该错误在下面的双引号中给出

  

" System.Net.Mail.SmtpException:发送邮件失败。 --->   System.Net.WebException:无法解析远程名称:   ' smtp.gmail.com'在System.Net.ServicePoint.GetConnection(PooledStream   PooledStream,Object owner,Boolean async,IPAddress&地址,插座&   abortSocket,Socket& abortSocket6,Int32 timeout)at   System.Net.PooledStream.Activate(Object owningObject,Boolean async,   Int32超时,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)   在System.Net.Mail.SmtpTransport.GetConnection(String host,Int32   在System.Net.Mail.SmtpClient.GetConnection()处的。)   System.Net.Mail.SmtpClient.Send(MailMessage消息)---内部结束   异常堆栈跟踪--- at   System.Net.Mail.SmtpClient.Send(MailMessage消息)at   emailtest._Default.Button1_Click(Object sender,EventArgs e)"

下面给出的行是我在按钮点击时调用的代码

    protected void SendMail()
    {
        // Gmail Address from where you send the mail
        var fromAddress = "myemailaddress@gmail.com";
        // any address where the email will be sending
        var toAddress = YourEmail.Text.ToString();
        //Password of your gmail address
        const string fromPassword = "myEmailAddressPassword";
        // Passing the values and make a email formate to display
        string subject = YourSubject.Text.ToString();
        string body = "From: " + YourName.Text + "\n";
        body += "Email: " + YourEmail.Text + "\n";
        body += "Subject: " + YourSubject.Text + "\n";
        body += "Question: \n" + Comments.Text + "\n";
        // smtp settings
        var smtp = new System.Net.Mail.SmtpClient();
        {
            smtp.Host = "smtp.gmail.com";
            smtp.Port = 587;
            smtp.EnableSsl = true;
            smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
            smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
            smtp.Timeout = 20000;
        }
        // Passing values to smtp object
        smtp.Send(fromAddress, toAddress, subject, body);
    }

 protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            //here on button click what will done 
            SendMail();
            DisplayMessage.Text = "Your Comments after sending the mail";
            DisplayMessage.Visible = true;
            YourSubject.Text = "";
            YourEmail.Text = "";
            YourName.Text = "";
            Comments.Text = "";
        }
        catch (Exception) { }
    }

请帮助我卡住。

2 个答案:

答案 0 :(得分:0)

这是我的代码:

// Send an email
            MailMessage EmailMsg = new MailMessage();
            EmailMsg.From = new MailAddress("youremail@outlook.com", "bc photo art lettering");
            EmailMsg.To.Add(new MailAddress("tothisemail@hotmail.com"));
            EmailMsg.Subject = "someone made an order";
            EmailMsg.Body = "<html><body><div style='border-style:solid;border-width:5px;border-radius: 10px; padding-left: 10px;margin: 20px; font-size: 18px;'> <p style='font-family: Vladimir Script;font-weight: bold; color: #f7d722;font-size: 48px;'> blah blah blah</p><hr><div width=40%;> <p  style='font-size: 20px;'>"Hello" +
                "</div></body></html>";
            EmailMsg.IsBodyHtml = true;
            EmailMsg.Priority = MailPriority.Normal;
            SmtpClient MailClient = new SmtpClient("smtp.live.com", 587);
            MailClient.EnableSsl = true;
            MailClient.Credentials = new System.Net.NetworkCredential("yourmail@outlook.com", "yourpassword");
            MailClient.Send(EmailMsg);

答案 1 :(得分:0)

    var receiver = new MailAddress("foo@bar.com");
    var sender = new MailAddress("bar@foo.com"); 
    var mail = new MailMessage(receiver , sender ); 
    mail.Subject = "Subject";
    mail.Body = "Body";

    var smtp = new SmtpClient();
    smtp.Host = "smtp.gmail.com";
    smtp.Port = 587; //Should be working
    smtp.Credentials = new NetworkCredential("username@gmail.com", password");
    smtp.EnableSsl = true;
    smtp.Send(mail);