我想从outlook-addin发送smtp发送邮件,将邮件保存到outlook发送文件夹
注意:从地址保存已发送文件夹中的邮件必须是我从地址而不是Outlook登录用户名指定的smtp。
public bool SendEMail()
{
MailMessage mailNew = new MailMessage();
var smtp = new SmtpClient("SmtpServer")
{
EnableSsl = false,
DeliveryMethod = SmtpDeliveryMethod.Network
};
smtp.Port = 587;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.UseDefaultCredentials = false;
System.Net.NetworkCredential credentials =
new System.Net.NetworkCredential("UserName", "password");
smtp.EnableSsl = false;
smtp.Credentials = credentials;
MailAddress mailFrom = new MailAddress("clark@gmail.com");
mailNew.From = mailFrom;
mailNew.To.Add("someone@gmail.com");
mailNew.Subject = Subject;
mailNew.IsBodyHtml = Html;
mailNew.Body = Body;
smtp.Send(mailNew);
return true;
}
提前致谢
答案 0 :(得分:2)
您需要在已发送邮件文件夹中创建已发送邮件,并使用MailItem.PropertyAccessor设置所有相关属性。请注意,PropertyAccessor不允许您设置一些与发件人相关的属性。在Outlook 2010或更高版本中,您可以设置MAilItem.Sender属性。
另请注意,保存邮件后无法更改已发送标志,因此要使用OOM创建已发送邮件,您需要创建邮件项,然后将其MessageClass属性更改为IPM.Note。
另请注意,每次保存邮件时,OOM都会重置ReceivedTime和SentOn属性。
如果使用Redemption是一个选项,您可以执行以下操作(在我的头顶):
Redemption.RDOSession session = new Redemption.RDOSession();
session.MAPIOBJECT = Application.Session.MAPIOBJECT;
Redemption.RDOMail message = session.GetDefaultFolder(rdoDefaultFolders.olFolderSentMail).Items.Add("IPM.Note");
message.Sent = true;
message.Subject = "test";
message.Body = "fake sent message";
message.Recipients.AddEx("The Recipient", "recipient@domain.com", "SMTP", olTo);
string senderEntryID = session.AddressBook.CreateOneOffEntryID("Some Name", "SMTP", "user@domain.com", false, true);
addressEntry = session.AddressBook.GetAddressEntryFromID(senderEntryID);
message.Sender = addressEntry;
message.SentOnBehalfOf = addressEntry;
message.Save();