在client.send(消息)上发送邮件时出错?

时间:2013-12-13 11:46:58

标签: asp.net

我创建了一个带有以下代码的mail.aspx.cs

    protected void Page_Load(object sender, EventArgs e)
    {
        MailMessage message = new MailMessage();
        message.From = new MailAddress("sender@foo.bar.com");
        message.To.Add(new MailAddress("recipient1@foo.bar.com"));
        message.Subject = "This is my subject";
        message.Body = "This is the content";
        SmtpClient client = new SmtpClient();
        client.Send(message);
    }

和web.config设置

<system.net>
    <mailSettings>
      <smtp from="test@gmail.com">
        <network host="smtpserver1" port="25" userName="abc xyz" password="abc" defaultCredentials="true" />
      </smtp>
    </mailSettings>
  </system.net>

在下一行的页面加载上发送邮件时出现失败错误

 client.Send(message); mail sending fail,

我是asp.net的新手,指导我正确解决这个问题的方法。

3 个答案:

答案 0 :(得分:0)

message.From = new MailAddress("sender@foo.bar.com");

<mailSettings>
      <smtp from="nitinturankar3@gmail.com">

必须一样!

答案 1 :(得分:0)

您使用的是空格userName="NITIN TURANKAR"的用户名,这是错误的

答案 2 :(得分:0)

public string SendMail(string toList, string from, string ccList, string subject, string body)
{

    System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
    SmtpClient smtpClient = new SmtpClient();
    string msg = string.Empty;
    try
    {
        MailAddress fromAddress = new MailAddress(from);
        message.From = fromAddress;
        message.To.Add(toList);
        //if (attachments != "")
        //{
        //    message.Attachments.Add(new Attachment(attachments));
        //}
        if (ccList != null && ccList != string.Empty)
            message.Bcc.Add(ccList);
        message.Subject = subject;
        message.IsBodyHtml = true;
        message.Body = body;
        // We use gmail as our smtp client
        smtpClient.Host = "smtp.gmail.com";
        smtpClient.Port = 25;
        smtpClient.EnableSsl = false;
        smtpClient.UseDefaultCredentials = false;
        smtpClient.Credentials = new System.Net.NetworkCredential(
            "Email", "password");

        smtpClient.Send(message);
        msg = "Successful<BR>";

        string message1 = "Your Query has been Submited Successfully";

    }
    catch (Exception ex)
    {

    }
    return msg;
}

尝试将端口替换为587。