通过Gmail发送电子邮件非常简单,如下所示:
Sending email in .NET through Gmail
但我似乎无法找到一种方法来发送电子邮件作为对另一封电子邮件的回复。自动包含以前的消息。
有趣的代码:与发送电子邮件无关(在这里阅读,使用AE.Net.Mail库为imap客户端)
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();
}
答案 0 :(得分:1)
回复邮件非常简单。在大多数情况下,您只需创建回复邮件的方式与创建任何其他邮件的方式相同。只有一些细微差别:
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
正文)。
答案 1 :(得分:0)
只需接收收到的电子邮件,读取其数据,如发件人,主题,文本,并在创建回复时使用它们。回复的目的地应该是发件人的电子邮件地址,subject =“RE:”+ subject + incoming,至于文本,您可以使用您想要的任何方法。我认为没有任何内置函数可以回复电子邮件。你必须手动完成这些事情。 首先,您必须找到一些方法来获取电子邮件客户端邮箱的内容,例如outlook或手动从SMTP服务器下载最后X条消息。