I am currently working on a C# project where I am making my own SMTP Server. It is more a less working but I am trying to make it work where I can send an email to multiple recipients that are on different domains.
I did initially do it so I would create a MailMesssage object and run the following to add each recipient
MailMessage message = new MailMessage();
message.To.add("[email protected]");
message.To.add("[email protected]");
If the different domains but going through the google app servers, I would get the MX record which was ALT2.ASPMX.L.GOOGLE.COM. When sending the mail though with the recipients add above google would send an error back as they do not allow cross domain sending via one SMTP session.
Therefore I have reworked it so a separate email is sent for each recipient, I get the MX record for each domain so I end up as well with a different SMTP session. So there would only be one message.To.add for each recipient I received. What I am trying to do is add a header so it still shows that the email recipients are still going to [email protected] and [email protected].
Therefore as far as the MailMessage component is concerned there is one recipient but the headers show multiple recipients so when the received of the mail views the email in their client it shows all the recipients that the email went to.
Below is the code I have to send the email.
MXLookup mxLookup = new MXLookup();
List<string> recipients = addRecipientsToEmail(message.emailRecipients);
foreach (string recipient in recipients)
{
string domain = Classes.CommonTasks.getDomainFromEmail(recipient);
string[] mxRecords = mxLookup.getMXRecords(Classes.CommonTasks.getDomainFromEmail(domain));
if (mxRecords != null)
{
MailMessage composedMail = new MailMessage();
composedMail.From = new MailAddress(message.EmailFromAddress);
composedMail.To.Add(recipient);
composedMail.Subject = message.subject;
composedMail.Body = message.EmailBody;
composedMail.Headers.Add(getHeaders(recipients));
if (message.contentType.ToString().Contains("text/html"))
{
composedMail.IsBodyHtml = true;
}
SmtpClient smtp = new SmtpClient(mxRecords[0]);
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Port = 25;
if (Configuration.emailConfig.useSmtpMaxIdleTime)
{
smtp.ServicePoint.MaxIdleTime = 1;
}
library.logging(methodInfo, string.Format("Sending email via MX Record: {0}", mxRecords[0]));
smtp.Send(composedMail);
updateEmailStatus(message.emailID, EmailStatus.Sent);
library.logging(methodInfo, string.Format("Successfully sent email ID: {0}", message.emailID));
}
else
{
string error = string.Format("No MX Record found for domain: {0}", domain);
library.logging(methodInfo, error);
library.setAlarm(error, CommonTasks.AlarmStatus.Warning, methodInfo);
}
Below is my getHeaders function.
private NameValueCollection getHeaders(List<string> emailRecipients)
{
string headers = "";
NameValueCollection headersArray = new NameValueCollection();
foreach (string recipient in emailRecipients)
{
headers += string.Format("{0}, ", recipient);
}
headersArray.Add("To", headers);
return headersArray;
}
Thanks for any help you can provide.
smtp serverwhy are you sending through google? Rhetorical. Anyway, you have two choices. choice 1: Follow google.com's guidelines and send 1 message for each person. I'm sure they have spent a fair amount of time ensuring people don't get around their restriction. Choice 2: change email providers, or simply setup your own mail server. - NotMe