Gmail 以回复方式发送电子邮件
本文关键字:电子邮件 方式发 回复 Gmail | 更新日期: 2023-09-27 18:05:16
通过gmail发送emai lmail非常简单,如下所示:
通过 Gmail 在 .NET 中发送电子邮件
但是我似乎找不到一种方法来发送电子邮件作为对另一封电子邮件的回复。自动包含以前的消息。
有趣的代码:与发送电子邮件无关(阅读此处,使用 AE。用于 imap 客户端的 Net.Mail 库(
public GmailConnector()
{
StringBuilder sb1 = new StringBuilder();
using (var context = new SupportDataContext())
using (var client = new ImapClient("imap.gmail.com", "xxxx@gmail.com", "xxx", AuthMethods.Login, 993, true))
{
client.SelectMailbox("INBOX");
Console.WriteLine(client.GetMessageCount());
var mm = client.SearchMessages(SearchCondition.Seen(), false, true);
foreach (var m in mm)
{
if (m.Value == null)
continue;
var msg = m.Value;
var emailRef = msg.To.First().Address;
SupportThread supportThread = null;
if (emailRef.Contains("+"))
{
var supportThreadRef = emailRef.Substring(emailRef.IndexOf('+') + 1, emailRef.IndexOf('@') - emailRef.IndexOf('+') - 1);
var supportThreadId = long.Parse(supportThreadRef);
supportThread = context.SupportThreads.First(x => x.Id == supportThreadId);
}
else if (msg.Subject.Contains("RE:"))
{
var subjectRef = msg.Subject.Replace("RE:", "").Trim();
var tmpDate = msg.Date.AddDays(-7);
var tmpSupportThread = context.SupportThreads.FirstOrDefault(x => x.EntryDate < msg.Date && x.EntryDate > tmpDate && x.Title.Equals(subjectRef));
if (tmpSupportThread != null)
supportThread = tmpSupportThread;
}
if (supportThread == null)
{
supportThread = new SupportThread();
supportThread.Title = msg.Subject;
supportThread.Creator = msg.From.Address;
supportThread.CreatorName = msg.From.DisplayName;
supportThread.EntryDate = msg.Date;
}
var responseMessage = msg.AlternateViews.GetHtmlView().Body;
responseMessage.Substring(0, responseMessage.IndexOf(REPLY_SEPERATOR));
var tmpEmailMessage = new EmailMessage();
tmpEmailMessage.EntryDate = msg.Date;
tmpEmailMessage.InnerContent = responseMessage;
tmpEmailMessage.SenderEmail = msg.From.Address;
tmpEmailMessage.SenderDisplayName = msg.From.DisplayName;
tmpEmailMessage.Title = msg.Subject;
tmpEmailMessage.SupportThread = supportThread;
foreach (var attachment in m.Value.Attachments)
{
var tmpAttachment = new Attachment();
tmpAttachment.Data = attachment.GetData();
tmpAttachment.Name = attachment.Filename;
tmpAttachment.EmailMessage = tmpEmailMessage;
context.Attachments.InsertOnSubmit(tmpAttachment);
}
context.EmailMessages.InsertOnSubmit(tmpEmailMessage);
context.SubmitChanges();
}
}
var fromAddress = new MailAddress("from@gmail.com", "From Name");
var toAddress = new MailAddress("to@example.com", "To Name");
const string fromPassword = "fromPassword";
const string subject = "Subject";
const string body = "Body";
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};
using (var message = new System.Net.Mail.MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body,
})
{
smtp.Send(message);
}
Console.Read();
}
回复消息相当简单。在大多数情况下,您只需创建回复消息,就像创建任何其他消息一样。只有几个细微的区别:
- 在回复
- 消息中,如果要回复的邮件中尚不存在前缀,则需要在
Subject
标头前面加上"Re: "
(换句话说,如果您要回复Subject
为"Re: party tomorrow night!"
的邮件,则不会在它前面加上另一个"Re: "
(。 - 您需要将回复邮件的
In-Reply-To
标头设置为原始邮件中Message-Id
标头的值。 - 您需要将原始邮件的
References
标头复制到回复邮件的References
标头中,然后附加原始邮件的Message-Id
标头。 - 您可能希望在回复中"引用"原始邮件的文本。
如果此逻辑在代码中表示,它可能看起来像这样(我在此示例中使用 MailKit(:
public static MimeMessage Reply (MimeMessage message, bool replyToAll)
{
var reply = new MimeMessage ();
// reply to the sender of the message
if (message.ReplyTo.Count > 0) {
reply.To.AddRange (message.ReplyTo);
} else if (message.From.Count > 0) {
reply.To.AddRange (message.From);
} else if (message.Sender != null) {
reply.To.Add (message.Sender);
}
if (replyToAll) {
// include all of the other original recipients - TODO: remove ourselves from these lists
reply.To.AddRange (message.To);
reply.Cc.AddRange (message.Cc);
}
// set the reply subject
if (!message.Subject.StartsWith ("Re:", StringComparison.OrdinalIgnoreCase))
reply.Subject = "Re: " + message.Subject;
else
reply.Subject = message.Subject;
// construct the In-Reply-To and References headers
if (!string.IsNullOrEmpty (message.MessageId)) {
reply.InReplyTo = message.MessageId;
foreach (var id in message.References)
reply.References.Add (id);
reply.References.Add (message.MessageId);
}
// quote the original message text
using (var quoted = new StringWriter ()) {
var sender = message.Sender ?? message.From.Mailboxes.FirstOrDefault ();
quoted.WriteLine ("On {0}, {1} wrote:", message.Date.ToString ("f"), !string.IsNullOrEmpty (sender.Name) ? sender.Name : sender.Address);
using (var reader = new StringReader (message.TextBody)) {
string line;
while ((line = reader.ReadLine ()) != null) {
quoted.Write ("> ");
quoted.WriteLine (line);
}
}
reply.Body = new TextPart ("plain") {
Text = quoted.ToString ()
};
}
return reply;
}
注意:此代码假定message.TextBody
不为空。尽管可能性很小,但可能会发生这种情况(这意味着消息不包含text/plain
正文(。
只需获取传入的电子邮件,读出其数据,例如发件人,主题,文本,并在创建回复时使用它们。回复的目的地应该是发件人的电子邮件地址,主题="RE:"+主题+传入,至于文本,您可以使用任何您想要的方法。我认为没有任何用于回复电子邮件的内置功能。你必须手动做这些事情。首先,您必须找到某种方法来获取电子邮件客户端邮箱的内容,例如Outlook或手动从SMTP服务器下载最后的X封邮件。
- 检索原始电子邮件(通过 OLE 从 Outlook-> COM 对象检索 -> Outlook OLE 对象就像已安装的 Outlook 邮件仓库的用户代理(。谷歌如何做到这一点。或从SMTP下载最新邮件。
- 获得邮件内容后,创建一封包含传入邮件内容的电子邮件。然后发送它。如果您使用Microsoft OLE,那么您也可以通过 outlook ole 对象发送电子邮件。