I'm creating a new mail definition using the CreateMailMessage function of a MailDefinition. One of the required parameters is recipients. The documentation for this function states that recipients is to be a comma-separated list of recipients, however when I try to send a message to multiple users I am getting the following error:
An invalid character was found in the mail header: ','...
So it seems like this function is not working as intended. Normally I would add all the recipients to the mail message itself, but unfortunately the recipients parameter is required and cannot be left blank. Any ideas?
I got it working but unfortunately its more of a hack than anything.
I take one email address from the "to" field and set it as the recipient in CreateMailMessage, which returns a MailMessage instance.
I take the produced MailMessage and add all the email addresses in my MailAddressCollection by iterating through the construct. I also do this for all CC'd users.
Related
I want to implement automated email delivery tracking and I found out I could do this using the Delivery Status Notification and setting envelope id to track which email has been delivered/not delivered.
However I can't seem to find a way to set the envelope id parameter using the MailMessage class in C#. As I understand this parameter should go in the MAIL FROM header, something like:
MAIL FROM: RET=HDRS ENVID=QQ314159
I tried setting it using message.Headers.Add(), however when I receive the email and inspect its headers I don't see the envelope id in the 'From' header, as it gets it value from the message.From class property and I can't insert anything else than the email address there.
Can't find any information, anybody has some experience with this?
You can't actually do this with System.Net.Mail, but you can do it using MailKit:
http://www.mimekit.net/docs/html/M_MailKit_Net_Smtp_SmtpClient_GetEnvelopeId.htm
I'm using MailChimp for .NET sending mails, but the wrapper for the recipient doesn't contain a type, which the is necessary for sending massmails.
MailChimp.Types.Mandrill.Messages.Recipient recipient = new MailChimp.Types.Mandrill.Messages.Recipient(member.EMail, member.Name);
Is it me who has missed something?
Quoted from MailChimp. Scroll down to "Tips for creating your HTML Campaign".
MailChimp does not use a BCC field as each recipient on your list is hidden from all other recipients on your list. We deliver a completely separate copy of your email to each recipient on your list, allowing you to personalize your content for each recipient, track clicks and opens, and address each email to the recipient's name.
I decided to use MCAPI.NET and changed the source adding a attribute to Recipient with name "to".
Setting this attribute to value "BCC" solved the problem.
Note : MailChimp is a Newsletter sending software where Mandrill is a transactional mail sending component.
I have this help desk application which sends an e-mail to me with a specific issue. The user is required to input his e-mail address into a textbox. What I'd like to do is for the user to get a copy of the problem as well.
MailMessage req_mail = new MailMessage(reqMail.Text, "system.admin#home.com");
Where reqMail.Text is the user's e-mail address.
Any ideas why it doesn't work? Because I can see from who it is...
The overload of the MailMessage constructor you're using takes a from and a to address.
You've supplied the users email as the from and your email as to. AFAIK, there isn't a constructor for CC'ing or BCC'ing.
Instead you just need to do this:
req_mail.Bcc.Add(new MailAddress(reqMail.Text));
Or:
req_mail.CC.Add(new MailAddress(reqMail.Text));
If you prefer.
According to the constructor you're using that MailMessage is from the user, not to the user. If you want to send the message to the user then their address would need to be in the To or CC or BCC parts of the message. Something like:
var req_mail = new MailMessage(reqMail.Text, "system.admin#home.com");
req_mail.CC.Add(new MailAddress(reqMail.Text));
We've seen a few similar questions on StackOverflow before regarding System.Web.Helpers.Webmail.Send but I see no proper explanation for what's going on.
Regarding the to: parameter, the documentation says:
The email address of the recipient or recipients. Separate multiple recipients using a semicolon (;).
and I've seen answers saying "use a comma because the docs are wrong", or "use a semicolon", or "maybe it's an environment issue".
The code
WebMail.Send(
to: "joe.bloggs#mail.com,jane.bloggs#mail.com",
from: "no-reply#company.com",
subject: "Some Automated Email",
body: "<strong>Lorem Ipsum</strong>",
isBodyHtml: true
);
I've tried a few scenarios:
joe.bloggs#mail.com;jane.bloggs#mail.com
No emails recieved: An invalid character was found in the mail header: ';'.
joe.bloggs#mail.com; jane.bloggs#mail.com
Only the first recipient receives the email
joe.bloggs#mail.com,jane.bloggs#mail.com
both recieved the email
joe.bloggs#mail.com, jane.bloggs#mail.com
both recieved the email
joe.bloggs#mail.com, non-existant#mail.com
First recieved the email, but uncaught exception: Mailbox unavailable. The server response was: 5.7.1 Unable to relay
non-existant#mail.com, joe.bloggs#mail.com
No emails recieved: An invalid character was found in the mail header: ','.
Can anybody shed some light on this? I've actually had even more bizzare behaviour on a different server; I'm using Exchange for the above tests, but actually experienced different behaviour on hMailServer where joe.bloggs#mail.com,jane.bloggs#mail.com resulted in a silent failure with no server errors and no outgoing mail in hMailServer logs. On the system with hMailServer I have only had success with a single address.
This probably has to do with the variety of relays you are connecting to, and the variety of methods they accept. Not only do the delimiter characters matter to each specific mail server, but the e-mail addresses also do (since different relays will be configure to accept certain e-mails, and will throw a variety of error codes back for bad e-mails, which will in turn throw a variety of exceptions).
The System.Net.Mail namespace has a MailMessage object that contains MailAddressCollection objects for To, CC, and Bcc that you can just call Add() on for each recipient.
I have a library that sends mail (without a relay) that uses it (everything goes to Bcc), you can check the code out there. If you happen to use the library, be sure to keep your IP address in good reputation and make sure your DNS records are all setup the same way you would if you were a relay (PTR and A records all setup).
As I understand it, the mistake in the documentation is the likely scenario. I don't have this assembly, so I can't confirm it in ILSpy, but apparently the helper class simply uses System.Net.Mail. Following the four parameter overload through I get to this internal method.
internal Message(string from, string to) : this()
{
//...
this.to = new MailAddressCollection
{
to
}
}
As a result, it simply creates a new MailAddressCollection which requires a comma delimiter. At no point did the to string ever replace or manipulate a semi-colon (unless this is done within the Helper class but that doesn't appear to be the case).
I am implementing a mailing list using using .NET. As discussed in this answer, I need to send an email where the recipient of the envelope is different from the recipient in the To header. How to achieve this in C#? The SmtpClient and MailMessage classes in System.Net.Mail doens't seem to permit this.
I tried:
message.To.Add("list#example.com");
message.Headers["Envelope-to"] = "user#example.com";
but the mail doesn't get sent to what it is specified in the Envelope-to.
Any suggestions?
Adding an address to Envelope-To without adding it to To
You can use the MailMessage.Bcc property. Addresses added there will only appear in the Envelope-To, not in the mail's To:
message.Bcc.Add("user#example.com");
Adding an address to To without adding it to Envelope-To
Here, I'm quite sure you are out of luck. I've had a look at the System.Net.Mail namespace with ILSpy, and it looks like this is not possible. The To header of the mail is created out of the To property of the MailMessage (see Message.PrepareHeaders), and the same property is used to fill the Envelope-To of the mail (together with the Cc and Bcc properties, see SmtpClient.Send). Manually setting Headers["To"] won't help, since this value is overwritten with the contents of the To property (see Message.PrepareHeaders).
So, list#example.com will get a copy of the message. Depending on the configuration of your SMTP server, this might lead to a mail loop.