c# - 将带有附件的信息发送到一个电子邮件地址,尤其是使用谷歌
问题描述
我想在单击按钮时发送一封带有附件的电子邮件,但即使我已连接到互联网,它仍显示“发送邮件失败”我仍然无法弄清楚问题是什么
private void proto_Type_AI_(object sender, RoutedEventArgs e)
{
try
{
//Smpt Client Details
SmtpClient clientDetails = new SmtpClient();
clientDetails.Port = Convert.ToInt32(port_number.Text.Trim());
clientDetails.Host = smtp_server.Text.Trim();
clientDetails.EnableSsl = ssl.IsChecked == true;
clientDetails.DeliveryMethod = SmtpDeliveryMethod.Network;
clientDetails.UseDefaultCredentials = false;
clientDetails.Credentials = new NetworkCredential(sender_email.Text.Trim(), sender_password.Password.Trim());
//message details
MailMessage maildetails = new MailMessage();
maildetails.From = new MailAddress(sender_email.Text.Trim());
maildetails.To.Add(recipent_email.Text.Trim());
//maildetails.Bcc.Add("bcc email address");
maildetails.Subject = subject.Text.Trim();
maildetails.IsBodyHtml = html.IsChecked == true;
maildetails.Body = body.Text.Trim();
clientDetails.Send(maildetails);
MessageBox.Show("HEY USER, YOUR MESSAGE HAS BEEN SENT");
}
catch (Exception ex)
{[![enter image description here][1]][1]
MessageBox.Show(ex.Message);
}
}
以下是我使用google smtp发送的输出图片
解决方案
using Microsoft.Office.Interop.Outlook;
namespace YourNamespace
{
public class YourClass
{
//Create a mail object
Microsoft.Office.Interop.Outlook.Application ol = new Microsoft.Office.Interop.Outlook.Application();
//Create a mail item
Microsoft.Office.Interop.Outlook.MailItem mailitem = ol.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
//Your mail subject text
mailitem.Subject = "SMART G's Auto Email Test";
//Mail Recipient
mailitem.To = "smartg@smartg.com";
//Mail Sub Recipient
mailitem.CC = "test@test.com";
//Your E-Mail Text
mailitem.Body = "Dear Sir or Madam,\n\n" +
"Attached please find my great manual on how to save the world. Thank you for reading. + "\n\n"
+ "Kind regards" + "\n\n" + "Smart G";
//Your mail attachment
mailitem.Attachments.Add(<<path>> + <<documentName>>);
//Do you want me to show you the mail?
mailitem.Display(true); //yes
//If you want to check the mail before sending it then do not use this.
mailitem.Send(); //automatically sends the mail before you can check it! (autosend)
//Notifcation
MessageBox.Show("Mail sent!")
}
}
推荐阅读
- ajax - 部署到 Azure 后 Ajax 无法正常工作
- wordpress - 如何使用 wp_query 仅在主要分类中获取帖子?
- markdown - 如何在 Markdown 的待办事项列表上标记失败的项目?
- java - 如何从 Firebase 检索数据以在 Android 地图上制作多个标记
- c# - C# DateTimeKind.Local 是指计算机还是业务对象?
- ecmascript-6 - 通过 gulp.js 将 ES6 转译为 ES5
- mysql - 如何优化与区域相关的 MYSQL 查询
- c# - 无法从 webapi 接收令牌并在 Angular 6 中显示
- javascript - 如何分别启用对所有循环图像的点击
- sql-server - 如何在 SELECT 中排除相同的行