我正在使用MVC3,需要向用户发送电子邮件。我不想使用gmail服务器。但是,我确实想使用服务器10.1.70.100。我不明白我做错了什么。这是我的代码:
var fromAddress = new MailAddress("sum1@abc.com", "From Name"); var toAddress = new MailAddress(EmailID, "To Name");
const string fromPassword = "";//To be Filled
const string subject = "Verification Mail";
string body = "You have successfully registered yourself. Please Enter your Verification code " + code.ActivatedCode;
var smtp = new SmtpClient
{
Host = "10.1.70.100",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
Credentials = new NetworkCredential(),
Timeout = 100000
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body
})
{
smtp.Send(message);
}
有人可以提出一种我无需提供凭据的方法吗?
答案 0 :(得分:2)
我更喜欢这样做:
的Web.config:
<system.net>
<mailSettings>
<smtp from="Description">
<network host="your.smtpserver" password="" userName="" />
</smtp>
</mailSettings>
</system.net>
您的代码:
var smtpClient = new SmtpClient();
smtpClient.Send(mail);
答案 1 :(得分:1)
对于我们的OSS项目,我们使用这个小助手。希望它有所帮助。
public void SendEmail(string address, string subject, string message)
{
string email = ConfigurationManager.AppSettings.Get("email");
string password = ConfigurationManager.AppSettings.Get("password");
string client = ConfigurationManager.AppSettings.Get("client");
string port = ConfigurationManager.AppSettings.Get("port");
NetworkCredential loginInfo = new NetworkCredential(email, password);
MailMessage msg = new MailMessage();
SmtpClient smtpClient = new SmtpClient(client, int.Parse(port));
msg.From = new MailAddress(email);
msg.To.Add(new MailAddress(address));
msg.Subject = subject;
msg.Body = message;
msg.IsBodyHtml = true;
smtpClient.EnableSsl = true;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = loginInfo;
smtpClient.Send(msg);
}