【发布时间】:2017-06-09 16:41:54
【问题描述】:
我正在制作一个可以发送电子邮件和阅读电子邮件的小程序。我目前可以发送电子邮件,但我不确定如何使用 .Net.Mail 访问我的收件箱。有没有办法做到这一点?
我的代码是休闲的
try
{
SmtpClient mySmtpClient = new SmtpClient("smtp.live.com");
// set smtp-client with basicAuthentication
mySmtpClient.UseDefaultCredentials = false;
System.Net.NetworkCredential basicAuthenticationInfo = new
System.Net.NetworkCredential("example@live.com", "password");
mySmtpClient.Credentials = basicAuthenticationInfo;
mySmtpClient.EnableSsl = true;
// add from,to mailaddresses
MailAddress from = new MailAddress("example@live.com");
MailAddress to = new MailAddress("example@example.eu");
MailMessage myMail = new System.Net.Mail.MailMessage(from, to);
MailMessage msg;
// set subject and encoding
myMail.Subject = "Test message";
myMail.SubjectEncoding = System.Text.Encoding.UTF8;
// set body-message and encoding
myMail.Body = "<b>Test Mail</b><br>using <b>HTML</b>.";
myMail.BodyEncoding = System.Text.Encoding.UTF8;
// text or html
myMail.IsBodyHtml = true;
mySmtpClient.Send(myMail);
}
catch (SmtpException ex)
{
throw new ApplicationException
("SmtpException has occured: " + ex.Message);
}
【问题讨论】:
-
你测试了吗?有什么问题?
-
如果您想访问存储的 Microsoft 邮件帐户的电子邮件文件夹,请考虑使用 REST API @msdn.microsoft.com/en-us/office/office365/api/… - POP 不会削减它。
-
框架的 System.Net.Mail 无法做到这一点。你需要一个像 MailKit 这样支持 IMAP 或 POP3 的库。
标签: c# email system.net.mail