I'm trying to add a List-Unsubscribe header to my e-mail that is being sent. So far I hadn't any luck trying to do so.
What I have got so far:
var mailMessage = new MailMessage
{
Subject = newsletter.Subject,
Body = newsLetterHTML,
IsBodyHtml = true,
Sender = new MailAddress(senderAddress)
};
mailMessage.To.Add(subscriber.Email);
mailMessage.ReplyToList.Add(senderAddress);
mailMessage.Headers.Add("List-Unsubscribe", unSubscribeUrl);
The unSubscribeUrl is something like 'www.example.com/unlist?id=8822772727'.
When I sent the e-mail everything works fine except for the list-unsubscribe option. Which is not shown in any mail client.
Any assistance would be welcome!
UPDATE
This is the whole code I use for sending the email:
var mailMessage = new MailMessage
{
Subject = newsLetter.Subject,
Body = newsLetterHTML,
IsBodyHtml = true,
Sender = new MailAddress(senderAddress)
};
mailMessage.To.Add(subscriber.Email);
mailMessage.ReplyToList.Add(senderAddress);
mailMessage.Headers.Add("List-Unsubscribe", String.Format("<{0}>", "http://www.foo.com/unlist?id=8888"));
mailMessage.HeadersEncoding = Encoding.Default;
var smtpClient = new SmtpClient();
smtpClient.Send(mailMessage);
UPDATE 2
After a little research I got the header into the mailMessage. When I sent an email I can see the following headers:
List-Unsubscribe: <http://demo.localhost/home/hobbymap-gerard/afmelden-voor-nieuwsbrief?id=c786aeb0-554d-4670-94d8-82d6f25a050b>
MIME-Version: 1.0
From: info#test.nl
To: test#gmail.com
Reply-To: info#test.nl
Date: 8 Feb 2011 09:50:22 +0100
Subject: Test met plaatje
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: quoted-printable
But when I open the email in any client I can't see the unsubscribe button in the client? Am I doing something else wrong?
Most email clients only support mailto-links.
Try this, it should work always:
mailMessage.Headers.Add("List-Unsubscribe", "<mailto:list#host.com?subject=unsubscribe>");
The RFC specifies it is possible to use http-links also.
But i've found that if you include http-links, the email clients no longer shows the unsubscribe link. I think it's because of the possibility that people only have access to the mail protocol.
So this does not always work:
mailMessage.Headers.Add("List-Unsubscribe", "<http://www.host.com/list.cgi?cmd=unsub&lst=list>, <mailto:list-request#host.com?subject=unsubscribe>";
According to the List-Unsubscribe website, the URL should be wrapped with angle brackets, e.g. <https://www.example.com/unlist?id=8822772727>.
You can try something like:
mailMessage.Headers.Add("List-Unsubscribe", String.Format(
CultureInfo.InvariantCulture, "<https://{0}>", unSubscribeUrl));
To ensure you are not flagged as spam make sure to have an SSL Certified domain.
In addition to other answers, there is also RFC-8058 that requires another header to enable HTTPS link unsubscribe functionality:
List-Unsubscribe:<https://example.com/unsubscribe.html?opaque=123456789>
List-Unsubscribe-Post: List-Unsubscribe=One-Click
List-Unsubscribe-Post header should have a value List-Unsubscribe=One-Click. This is to prevent accidental unsubscribe by anti-spam software and allow an extra step of displaying a web page with an unsubscribe button.
Some email client will not process List-Unsubscribe links without List-Unsubscribe-Post header.
Related
I am having an issue with sending emails specifically to Gmail-related accounts, and I'll be darned if I know what the issue is. This is a C# ASP.NET project, by the way.
First, the following code works, as long as I am sending to any email account OTHER than a Gmail account:
var mail = new MailMessage {
Subject = "test email",
Body = "this is only a test",
Priority = MailPriority.High,
IsBodyHtml = true,
From = new MailAddress ( "<outbound email here>" )
};
var msgID = Guid.NewGuid().ToString();
var sentBy="<outbound mail domain>";
mail.Headers.Add ( "message-id", $"<{msgID}>");
mail.Headers.Add ( "msg-id", $"<{msgID}#{sentBy}>");
mail.To.Add ( new MailAddress ( "<recipient email>" ) );
var smtpClient = new SmtpClient("<email server address>") {
Port = 587,
Credentials = new NetworkCredential("<sender's email address>", "<password>"),
};
smtpClient.Send ( mail );
I have removed email addresses and network credentials, obviously.
The code works, because as long as I send email to a NON-Gmail account, it comes through just fine. But anything going to a Gmail-related account never arrives.
I added the two lines in the code above to add a message ID to the header based on what I read in several older posts here about some mail servers, like Gmail, rejecting email messages that didn't include them, but it has not fixed the issue, and I'm out of ideas. My ISP says the SPF record for the mail server is fine, so according to them that's not the issue.
Has anyone else encountered this recently, and if so, how did you fix it?
To clarify, the comments/answers I have received so far are appreciated, but as I stated in the OP, this is a problem with sending emails TO Gmail accounts. I am using my ISP's mail server to send them, and I am adding a message ID to the header to address what the log says, that the message is missing a message ID and won't be accepted. I can send emails to other non Gmail accounts just fine, and when I inspect the headers they show a message id. So I don't know why this continues to be an issue.
So the answer to this is that the issue was related to changes Google made to policies for sending emails to Gmail accounts, which might require adjustments to the SPF record on the sending SMTP server. That was the case in this situation - the hosting company was slow to respond (took them more than a week) and had to elevate the issue to their Tier 3 techs, but once the SPF record was fixed to account for Google's changes, all was resolved.
I tried sending mails with the help of send grid mail service using .Net core.
var client = new SendGridClient(xxxxxxxxxxxxxxxxxxxxxx);
var from = new EmailAddress("test#abc.com", "Not User");
var subject = "TestMail";
var to = new EmailAddress("test#abc.com, "Example User");
var plainTextContent = "This is body";
var htmlContent = "";
var msg = MailHelper.CreateSingleEmail(from, to, subject, plainTextContent, htmlContent);
client.SendEmailAsync(msg).Wait();
The mails sent using this code are not visible in sent items of my mail box. What is the possible solution to this? Thanks in advance.
When you send the email it uses SMTP directly to sendgrid. When you send via Google they will automatically add it to your outbox. To do the same when someone else sends the message you would have to manually place a copy of the sent message in your outbox using IMAP.
What is the possible solution to this?
You could use SendGrid's BCC option will allow you to BCC an email address, in this case your mail account, with every email sent.
Go to your SendGrid Account>Manage>Settings>Mail Settings and turn on the BCC option.
Note: With this setting turned on, you will be charged an extra email for every email you send. So reclick on Manage and you will see the popup message.
Here is Send mailbox snapshot:
Introdution
I am currently working a system develop with C#.
The system is about request approval.
When a request made, system will send email to user ask for response.
User's response will as simple as approve, reject or request update.
Question/Problem
Is it possible to have a button (approve or reject) in email content which allow user to response to system with only one click but without open browser?
Or, Is it possible to create button in email content which enable user to click to create new email with pre-set subject and recipient like following:
subject: request id - 123 - action - approve
to: response#system.com
as response email for user to send.
Then system can then recognize the email received and perform required back-end process.
Research Done
Research 1
What I currently found was outlook appointment email.
it done like second solution create new email with content for user send a response.
But, it only have options accept, decline and tentative.
And, I am not sure is blackberry support it like outlook.
The following is the blog found to create appointment email:
http://chuckdotnet.blogspot.my/2007/10/send-outlook-meeting-requests-with.html
Research 2
The following website teach you how to create hyperlink in email content which can create new email with pre-populate subject, body, and recipient
https://community.articulate.com/discussions/building-better-courses/hyperlink-to-create-an-already-written-email
However, No test had perform in blackberry yet.
Appreciate for any suggestion from you guys and I willing to try.
Is it possible to sent an email with button which can click to create
email with some pre-set content?
Yes, this is possible, see the System.Net.Mail code in the .NET framework.
https://msdn.microsoft.com/en-us/library/system.net.mail(v=vs.110).aspx
You can also see this StackOverflow question about how this is used.
Send e-mail via SMTP using C#
MailMessage mail = new MailMessage("you#yourcompany.com", "user#hotmail.com");
SmtpClient client = new SmtpClient();
client.Port = 25;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Host = "smtp.google.com";
mail.Subject = "this is a test email.";
mail.Body = "this is my test email body";
client.Send(mail);
Finally, your code should fire a serverside post on button click to have the server take care of sending the email. If data needs to be posted to the server, you may want to consider putting this data in a form input, which will post to your controller. You can then take that data and build the email with the example I provided in the links.
I want to send a password email to a user, however the customer wants an image embedded (inline) in the email.
I created an email, saved the data to a txt file, during my code I read in the template but when I send it the line endings are broken and therefore the MIME data is broken. I just get =3D
What am I doing wrong?
string FILENAME = Server.MapPath("~/GuestUserTemplate.txt");
StreamReader objStreamReader = File.OpenText(FILENAME);
string sEmailTemplate = "";
string input = null;
while ((input = objStreamReader.ReadLine()) != null)
{
sEmailTemplate = sEmailTemplate + input;
}
objStreamReader.Close();
/* send an email */
MailMessage msg = new MailMessage();
msg.IsBodyHtml = true;
msg.To.Add(new MailAddress(sToEmail));
msg.From = new MailAddress(sFromEmail);
msg.Subject = sEmailSubject;
msg.Body = sEmailTemplate;
//try
{
client.Send(msg);
}
//catch (Exception excm)
{
}
Just done a bit more detective work. The email I am sending out has this in the header:
MIME-Version: 1.0
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: quoted-printable
Where as an email which has inline images has:
Content-class: urn:content-classes:message
MIME-Version: 1.0
Content-Type: multipart/related;
boundary="----_=_NextPart_001_01C9C98D.6552117E";
type="multipart/alternative"
It seems that I need to set the Content type to multipart but I am not sure how...
I'm not quite sure what kind of text you are loading (and appending to what?) but I would recommend you create a real template, e.g. your email text with placeholders that will be replaced with user's name, etc.
Use <img src="cid:logo.png" /> - for the inline image in the HTML body of the message (in your template).
You will then need to add the corresponding image to the LinkedResources collection of the MailMessage and set its ContentID header to "logo.png" or whatever you call it. After that go and send your mail (multipart content type will be set automatically for you based on the structure of the mail message).
P.S.: use SendAsync() or write the mail to the local pickup queue of your own smtp server, otherwise you tie up your ASP.NET worker thread. Connecting to remote smtp servers/web services etc. takes a considerable amount of time (compared to the request execution time) and the worker thread is sitting there waiting and unable to service other incoming requests.
You should use System.Net.Mail. Create a MailMessage, then add an attachment and send with SmtpClient.
Check your text file - it may be missing the expected line ending (Cr + Lf).
Change the while loop from:
while ((input = objStreamReader.ReadLine()) != null)
{
sEmailTemplate = sEmailTemplate + input;
}
To:
sEmailTemplate = sEmailTemplate + objStreamReader.ReadToEnd();
The bottom line is that you can't do this with System.Net.Mail.
Here is the problem you are running into.
Encodings.
Your orignal mail was saved with the quoted-printable encoding (hence the "=3D"s you are seeing) When you re-read this into the message, these will get double encoded. Encodings are using for protecting the message during SMTP transport.
Boundaries.
When a message is created, boundaries are used in the headers to tell the mail client the boundaries of different parts of the message. You are trying to take boundaries of one message, and merge them into a new message. Since System.Net.Mail doesn't give you enough control over the internal boundaries, you can't do this.
If you want to send an embedded image using System.Net.Mail, you will need to create the message using a Linked Resource. Here is a link with more:
http://systemnetmail.com/faq/4.4.aspx
When you send an email using C# and the System.Net.Mail namespace, you can set the "From" and "Sender" properties on the MailMessage object, but neither of these allows you to make the MAIL FROM and the from address that goes into the DATA section different from each other. MAIL FROM gets set to the "From" property value, and if you set "Sender" it only adds another header field in the DATA section. This results in "From X#Y.COM on behalf of A#B.COM", which is not what you want. Am I missing something?
The use case is controlling the NDR destination for newsletters, etc., that are sent on behalf of someone else.
I am currently using aspNetEmail instead of System.Net.Mail, since it allows me to do this properly (like most other SMTP libraries). With aspNetEmail, this is accomplished using the EmailMessage.ReversePath property.
MailMessage.Sender will always insert a Sender header (interpreted as on behalf of in your e-mail client).
If you use the Network delivery method on the SmtpClient, .Sender will also change the sender in the envelope. Using the PickupDirectoryFromIis delivery method will leave it to IIS to determine the envelope sender, and IIS will use the From address, not the Sender address.
There's a similar question on MSDN here.
I just found how to do it:
mail.From specify the email from visible to the final user
mail.Sender specifies the envelope MAIL FROM
That's it (even if it took me a while to figure it out)
If you add the following lines the Return-Path and the Reply-To headers are set in the mail header.
Dim strReplyTo As String = "email#domain.tld"
message.ReplyToList.Add(strReplyTo)
message.Headers.Add("Return-Path", strReplyTo)
And if you click on reply the e-mail set to the Reply-To address
Do you mean this?:
//create the mail message
MailMessage mail = new MailMessage();
//set the addresses
mail.From = new MailAddress("me#mycompany.com");
mail.To.Add("you#yourcompany.com");
//set the content
mail.Subject = "This is an email";
mail.Body = "this is a sample body with html in it. <b>This is bold</b> <font color=#336699>This is blue</font>";
mail.IsBodyHtml = true;
//send the message
SmtpClient smtp = new SmtpClient("127.0.0.1");
smtp.Send(mail);
From http://www.systemnetmail.com/faq/3.1.2.aspx