如何发送电子邮件
本文关键字:电子邮件 何发送 | 更新日期: 2023-09-27 18:15:59
我是Asp.net
的新手,我需要使用我的Outlook
从Asp.net
发送电子邮件。我有一个按钮在asp,当我点击按钮(发送)我想发送电子邮件。我尝试使用Hotmail和Gmail
,但远程服务器被阻止。如果你不明白我的问题,请告诉我。我试过了:
var smtpClient = new SmtpClient
{
Host = "outlook.mycompany.local",
UseDefaultCredentials = false,
Credentials = new NetworkCredential("myEmail@mycommpany.com", "myPassword")
};
var message = new System.Net.Mail.MailMessage
{
Subject = "Test Subject",
Body = "FOLLOW THE WHITE RABBIT",
IsBodyHtml = true,
From = new MailAddress("myemail@mycommapny.com")
};
// you can add multiple email addresses here
message.To.Add(new MailAddress("friendEmail@Company.com"));
// and here you're actually sending the message
smtpClient.Send(message);
}
exception Show: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.1 Client was not authenticated
从ASP.net网站发送出站电子邮件可能会有问题。即使您获得了正确的SMTP信息,您仍然需要处理:
- 发件人策略框架(SPF)
- 白名单和黑名单 <
- 验证/gh>
- 反弹
你自己很难做到这一点,这就是为什么你可能想要考虑使用服务提供者。您只需使用他们的API(通常是一个REST调用),其余的由他们来完成。以下是三个这样的提供者:
- SendGrid
- 山魈
- Mailgun
Mandrill有一个低端的免费计划,如果你在Windows Azure上使用它,SendGrid也是如此。而且它们都是相当实惠的,即使是更大的计划。
我强烈建议使用其中一个带有自己的API,而不是自己使用System.Net.Mail
。但是如果你愿意,他们也可以为你充当SMTP中继,这样你就可以使用他们的SMTP服务器并保持你的System.Net.Mail
代码完整。
首先获取公司SMTP服务器设置(我猜是从你的系统管理员),然后你可以做这样的事情:
// setting up the server
var smtpClient = new SmtpClient
{
Host = "your.company.smtp.server",
UseDefaultCredentials = false,
EnableSsl = true, // <-- see if you need this
Credentials = new NetworkCredential("account_to_use", "password")
};
var message = new MailMessage
{
Subject = "Test Subject",
Body = "FOLLOW THE WHITE RABBIT",
IsBodyHtml = true,
From = new MailAddress("from@company.com")
};
// you can add multiple email addresses here
message.To.Add(new MailAddress("neo@matrix.com"));
// and here you're actually sending the message
smtpClient.Send(message);
可以使用这个函数。还有一件事你必须将你的电子邮件SMTP登录名和密码存储在web配置文件
中/// <summary>
/// Send Email
/// </summary>
/// <param name="strFrom"></param>
/// <param name="strTo"></param>
/// <param name="strSubject"></param>
/// <param name="strBody"></param>
/// <param name="strAttachmentPath"></param>
/// <param name="IsBodyHTML"></param>
/// <returns></returns>
public Boolean sendemail(String strFrom, string strTo, string strSubject, string strBody, string strAttachmentPath, bool IsBodyHTML)
{
Array arrToArray;
char[] splitter = { ';' };
arrToArray = strTo.Split(splitter);
MailMessage mm = new MailMessage();
mm.From = new MailAddress(strFrom);
mm.Subject = strSubject;
mm.Body = strBody;
mm.IsBodyHtml = IsBodyHTML;
//mm.ReplyTo = new MailAddress("replyto@xyz.com");
foreach (string s in arrToArray)
{
mm.To.Add(new MailAddress(s));
}
if (strAttachmentPath != "")
{
try
{
//Add Attachment
Attachment attachFile = new Attachment(strAttachmentPath);
mm.Attachments.Add(attachFile);
}
catch { }
}
SmtpClient smtp = new SmtpClient();
try
{
smtp.Host = ConfigurationManager.AppSettings["MailServer"].ToString();
smtp.EnableSsl = true; //Depending on server SSL Settings true/false
System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
NetworkCred.UserName = ConfigurationManager.AppSettings["MailUserName"].ToString();
NetworkCred.Password = ConfigurationManager.AppSettings["MailPassword"].ToString();
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 587;//Specify your port No;
smtp.Send(mm);
return true;
}
catch
{
mm.Dispose();
smtp = null;
return false;
}
}
试试Amazon Simple Email Service (http://aws.amazon.com/ses/)。如果您是亚马逊网络服务(AWS)的新手,可能会有一个学习曲线。然而,一旦你熟悉了他们的SDK(可以在Nuget上找到),这个过程就非常简单了(Amazon确实有很多奇怪的小包装类)。
那么,要回答"如何发送电子邮件?"这个问题,它看起来像这样:
var fromAddress = "from@youraddress.com";
var toAddresses = new Amazon.SimpleEmail.Model.Destination("someone@somedestination.com");
var subject = new Amazon.SimpleEmail.Model.Content("Message");
var body= new Body(new Amazon.SimpleEmail.Model.Content("Body"));
var message = new Message(subject , body);
var client = ConfigUtility.AmazonSimpleEmailServiceClient;
var request= new Amazon.SimpleEmail.Model.SendEmailRequest();
request.WithSource(fromAddress)
.WithDestination(toAddresses)
.WithMessage(message );
try
{
client.SendEmail(request);
}
catch (Amazon.SimpleEmail.AmazonSimpleEmailServiceException sesError)
{
throw new SupplyitException("There was a problem sending your email", sesError);
}
您可以参考以下链接:
http://www.codeproject.com/Tips/371417/Send-Mail-Contact-Form-using-ASP-NET-and-Csharp发送邮件asp.net c#
我希望它能帮助你。:)