Send Error:不允许使用邮箱名称。 服务器响应为: You are not authorized to send mail, authentication is required
使用asp.net 2.0 中新的System.Net.Mail发送邮件,使用163邮件服务器时等其他服务器时可能会出现:“Send Error:不允许使用邮箱名称。 服务器响应为: You are not authorized to send mail, authentication is required”错误,以下程序中红色为特别需要主要的地方。 protected void Button1_Click(object sender, EventArgs e)
...{
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
//收件人
message.To.Add(txtMailAddress.Text);
//抄送
foreach(string mailAddress in txtCopyAddress.Text.Split(','))
...{
if (!string.IsNullOrEmpty(mailAddress))
...{
message.CC.Add(mailAddress);
}
}
//主题
message.Subject = txtSubject.Text;
message.From = new System.Net.Mail.MailAddress(fromAddress);
message.Body = txtMailContents.Text;
//发送附件
if (fulAttachment.HasFile)
...{
System.Net.Mail.Attachment attachment = new System.Net.Mail.Attachment(fulAttachment.PostedFile.FileName);
message.Attachments.Add(attachment);
}
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient(txtSMTPAddress.Text);
smtp.Credentials = new System.Net.NetworkCredential(txtUserName.Text,txtPassword.Text);
//smtp.usedefaultcredentials = true;
try
...{
smtp.Send(message);
lblReturnMessage.Text = "Mail Send Success!";
}
catch (System.Net.Mail.SmtpException ex)
...{
lblReturnMessage.Text = "Send Error:" + ex.Message;
}
}
如果选择了163的SMTP服务器,“smtp.163.com”,发件人必须是登录用户的163 email用户地址。
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("smtp.163.com");
smtp.Credentials = new System.Net.NetworkCredential("163emailaddress","emailpassword");
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.From = new System.Net.Mail.MailAddress("163emailaddress@163.com");
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/licerfordream/archive/2007/04/26/1586465.aspx