需要设置可以通过指定邮件服务器进行relay的ip地址
smtp->properties->access->relay
add the ip address
public bool Send(string to, string from, string subject, string message)
{
try
{
MailMessage em = new MailMessage();
em.To = to;
em.From = from;
em.Subject = subject;
em.Body = message;
//Found out how to send authenticated email via System.Web.Mail at http://SystemWebMail.com (fact 3.8)
if(this.UserName != null && this.Password != null)
{
em.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //basic authentication
em.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", this.UserName); //set your username here
em.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", this.Password); //set your password here
}
SmtpMail.SmtpServer = this.SmtpServer;
SmtpMail.Send(em);
return true;
}
catch
{
return false;
}
}
}
}
博客给出了一段邮件发送的代码示例。代码中创建邮件消息对象,设置收件人、发件人、主题和正文等信息。若用户名和密码不为空,会添加认证字段。最后指定 SMTP 服务器并尝试发送邮件,若发送失败则返回 false。
1415

被折叠的 条评论
为什么被折叠?



