Creates a reply message by inserting a new Textpart. Leaves the original parts unmodified. Replies to the sender and CC's all recipients.
In this example, the Pop component gets only messages with specific subject lines.
It then creates and queues "reply" and "forward" response messages for these messages.
//Queued response messages ready for sending
List<MailMessage> queuedMessages = new List<MailMessage>();
private void autoReply(object sender)
{
//Configure server and account info
pop1.Session.RemoteEndPoint = new Dart.Mail.IPEndPoint(myServer, Pop.GetDefaultPort(pop1.Session));
pop1.Session.Username = myUsername;
pop1.Session.Password = myPassword;
//Connect and log into the account
pop1.Connect();
pop1.Authenticate(true, true);
//Get header for each message in the account and create response according to subject
foreach (PopMessage popMessage in pop1.Messages)
{
popMessage.Get(0);
string code = popMessage.Message.Subject;
//If message contains a specific subject, get entire message then queue an automated response message
switch (code)
{
case "ALERT":
popMessage.Get();
//Create a reply message and add it to a list of messages to be sent
MailMessage replyMessage = popMessage.Message.Reply(fromAddress, "Message acknowledged.");
queuedMessages.Add(replyMessage);
break;
case "ALERT ACK ALL":
popMessage.Get();
//Create a reply-all message and add it to a list of messages to be sent
MailMessage replyAllMessage = popMessage.Message.ReplyAll(fromAddress, "Message acknowledged to all.");
queuedMessages.Add(replyAllMessage);
break;
case "ALERT ELEVATE":
popMessage.Get();
//Create a forward message and add it to a list of messages to be sent
MailMessage forwardMessage = popMessage.Message.Forward(fromAddress, supervisorAddress, "Elevated. Please see message below.", ForwardFormat.Standard);
queuedMessages.Add(forwardMessage);
break;
case "ALERT INFORM":
popMessage.Get();
//Create a redirect message and add it to a list of messages to be sent
MailMessage redirectMessage = popMessage.Message.Forward(fromAddress, colleagueAddress, "Redirected. Please see included message.", ForwardFormat.Redirect);
queuedMessages.Add(redirectMessage);
break;
default:
break;
}
}
//Gracefully logout
pop1.Close();
//Send queued responses (not implemented in this snippet)
sendResponses();
}
private void sendResponses()
{
//Configure server and account info
smtp1.Session.RemoteEndPoint = new IPEndPoint(myServer, Smtp.GetDefaultPort(smtp1.Session));
smtp1.Session.Username = myUsername;
smtp1.Session.Password = myPassword;
//Send all messages in the responses list
foreach (MailMessage message in queuedMessages)
smtp1.Send(message);
queuedMessages.Clear();
//Gracefully logout
smtp1.Close();
}
'Queued response messages ready for sending
Private queuedMessages As New List(Of MailMessage)()
Private Sub autoReply(ByVal sender As Object)
'Configure server and account info
pop1.Session.RemoteEndPoint = New Dart.Mail.IPEndPoint(myServer, Pop.GetDefaultPort(pop1.Session))
pop1.Session.Username = myUsername
pop1.Session.Password = myPassword
'Connect and log into the account
pop1.Connect()
pop1.Authenticate(True, True)
'Get header for each message in the account and create response according to subject
For Each popMessage As PopMessage In pop1.Messages
popMessage.Get(0)
Dim code As String = popMessage.Message.Subject
'If message contains a specific subject, get entire message then queue an automated response message
Select Case code
Case "ALERT"
popMessage.Get()
'Create a reply message and add it to a list of messages to be sent
Dim replyMessage As MailMessage = popMessage.Message.Reply(fromAddress, "Message acknowledged.")
queuedMessages.Add(replyMessage)
Case "ALERT ACK ALL"
popMessage.Get()
'Create a reply-all message and add it to a list of messages to be sent
Dim replyAllMessage As MailMessage = popMessage.Message.ReplyAll(fromAddress, "Message acknowledged to all.")
queuedMessages.Add(replyAllMessage)
Case "ALERT ELEVATE"
popMessage.Get()
'Create a forward message and add it to a list of messages to be sent
Dim forwardMessage As MailMessage = popMessage.Message.Forward(fromAddress, supervisorAddress, "Elevated. Please see message below.", ForwardFormat.Standard)
queuedMessages.Add(forwardMessage)
Case "ALERT INFORM"
popMessage.Get()
'Create a redirect message and add it to a list of messages to be sent
Dim redirectMessage As MailMessage = popMessage.Message.Forward(fromAddress, colleagueAddress, "Redirected. Please see included message.", ForwardFormat.Redirect)
queuedMessages.Add(redirectMessage)
Case Else
End Select
Next popMessage
'Gracefully logout
pop1.Close()
'Send queued responses (not implemented in this snippet)
sendResponses()
End Sub
Private Sub sendResponses()
'Configure server and account info
smtp1.Session.RemoteEndPoint = New IPEndPoint(myServer, Smtp.GetDefaultPort(smtp1.Session))
smtp1.Session.Username = myUsername
smtp1.Session.Password = myPassword
'Send all messages in the responses list
For Each message As MailMessage In queuedMessages
smtp1.Send(message)
Next message
queuedMessages.Clear()
'Gracefully logout
smtp1.Close()
End Sub