Sending an emai lthrough gmail is easy enough as seen below:
Sending email in .NET through Gmail
But I can't seem to find a method to send the email as a response to another email. Automatically including the previous messages.
Code for fun: Doesn't really relate to sending an email(reading here, using AE.Net.Mail library for imap client)
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();
}
Just take the incoming email, read out its data such as sender, subject, text and make use of them when creating the reply. The destination of the reply should be the sender's email address, subject = "RE:"+subject+incoming, as for the text you can use whatever methodology you want. I don't think that there is any built-in function for replying to an email. You gotta do those things manually. First of all you have to find some way to get out the contents of the mailbox of the email client, such as outlook or download the last X messages from the SMTP server manually.
Replying to a message is fairly simple. For the most part, you'd just create the reply message the same way you'd create any other message. There are only a few slight differences:
Subject
header with"Re: "
if the prefix doesn't already exist in the message you are replying to (in other words, if you are replying to a message with aSubject
of"Re: party tomorrow night!"
, you would not prefix it with another"Re: "
).In-Reply-To
header to the value of theMessage-Id
header in the original message.References
header into the reply message'sReferences
header and then append the original message'sMessage-Id
header.If this logic were to be expressed in code, it might look something like this (I'm using MailKit in this example):
Note: This code assumes that
message.TextBody
is not null. It's possible, although fairly unlikely, that this could happen (meaning that the message does not contain atext/plain
body).