We have a site from where we send a auto generated mail to our customer when a new customer register. our mail got a activation link which look like
http://www.bba-reman.com/catalogue/ConfirmRegistration.aspx?email=meyer#reman.de&id=907a5253-106c-4fb3-9882-83e634e651b2
but when our german customer reveive the mail then he got the below activation link where you notice & character change to ®
http://www.bba-reman.com/catalogue/ConfirmRegistration.aspx?email=meyer#reman.de®id=907a5253-106c-4fb3-9882-83e634e651b2
Can anyone tell me why & character getting change to ®?
How to resolve this kind of problem?
try using CreateAlternateViewFromString property, here is the example code
MailMessage emailmsg = new MailMessage("from#address.co.za", "to#address.co.za")
emailmsg.Subject = "Subject";
emailmsg.IsBodyHtml = false;
emailmsg.ReplyToList.Add("from#address.co.za");
emailmsg.BodyEncoding = System.Text.Encoding.UTF8;
emailmsg.HeadersEncoding = System.Text.Encoding.UTF8;
emailmsg.SubjectEncoding = System.Text.Encoding.UTF8;
emailmsg.Body = null;
var plainView = AlternateView.CreateAlternateViewFromString(EmailBody, emailmsg.BodyEncoding, "text/plain");
plainView.TransferEncoding = TransferEncoding.SevenBit;
emailmsg.AlternateViews.Add(plainView);
SmtpClient sSmtp = new SmtpClient();
sSmtp.Send(emailmsg);
Related
The official Gmail API documentation is horrendous. Not getting any clue to integrate Gmail API using .NET framework in vs2017. I wanted to send the input data of the Web form to a user's email.
It would be a three step process -
Define an HTML template which which describes how your mail should be presented.
Write a small c# code to replace all place holders like your form fields , user name, etc.
private string createEmailBody(string userName, string title, string message)
{
string body = string.Empty;
//using streamreader for reading my htmltemplate
using(StreamReader reader = new StreamReader(Server.MapPath("~/HtmlTemplate.html")))
{
body = reader.ReadToEnd();
}
body = body.Replace("{UserName}", userName); //replacing the required things
body = body.Replace("{Title}", title);
body = body.Replace("{message}", message);
//// Instead of message add you own parameters.
return body;
}
When form is submitted, call step 2 code first. Then use it's output to set mail body.
Code would be something like -
string smtpAddress = "smtp.gmail.com";
int portNumber = 587;
bool enableSSL = true;
/// This mail from can just be a display only mail I'd
string emailFrom = "no-reply#gmail.com";
string subject = "your subject";
string body = createEmailBody();
using (MailMessage mail = new MailMessage())
{
mail.From = new MailAddress(emailFrom);
mail.To.Add(emailTo);
/// Add more to IDs if you want to send it to multiple people.
mail.Subject = subject;
mail.Body = body;
mail.IsBodyHtml = true;
// This is required to keep formatting of your html contemts
/// Add attachments if you want, this is optional
mail.Attachments.Add(new Attachment(yourfilepath));
using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber))
{
smtp.Credentials = new NetworkCredential(your-smtp-account-email, your-smtp-account-password);
smtp.EnableSsl = enableSSL;
smtp.Send(mail);
}
}
Refer this link for working example
https://www.c-sharpcorner.com/UploadFile/33b051/sending-mail-with-html-template/
EDIT: For using GMail API
Using GMAIL APIs you will need two nuget packages:
1. Install-Package Google.Apis.Gmail.v1
2. Install-Package AE.Net.Mail
Code is very similar to what we have for normal SMTP mail send. It is explained at: http://jason.pettys.name/2014/10/27/sending-email-with-the-gmail-api-in-net-c/
I'm searching for an working C# example to add list-unsubscribe header with Amazon SES.
After reading that Amazon-SES now supports adding Headers I was searching for an C# example but was unable to find one.
I couldn't find nice API like they have for Java. For C#, I found two alternative.
The easiest option is probably to switch to the SMTP interface and .Net's native SMTP classes (or a third-party library): Send an Email Using SMTP with C#
The example code is using the MailMessage class from System.Net.Mail:
// Create and build a new MailMessage object
MailMessage message = new MailMessage();
message.IsBodyHtml = true;
message.From = new MailAddress(FROM,FROMNAME);
message.To.Add(new MailAddress(TO));
message.Subject = SUBJECT;
message.Body = BODY;
// Comment or delete the next line if you are not using a configuration set
message.Headers.Add("X-SES-CONFIGURATION-SET", CONFIGSET);
Another (less attractive) option is to use SendRawEmailRequest. With this API, you have to encode the message along with its headers, attachments, and other data, in a MemoryStream.
Example code, from the AWS SDK .Net documentation - SES - RawMessage:
var sesClient = new AmazonSimpleEmailServiceClient();
var stream = new MemoryStream(
Encoding.UTF8.GetBytes("From: johndoe#example.com\n" +
"To: janedoe#example.com\n" +
"Subject: You're invited to the meeting\n" +
"Content-Type: text/plain\n\n" +
"Please join us Monday at 7:00 PM.")
);
var raw = new RawMessage
{
Data = stream
};
var to = new List<string>() { "janedoe#example.com" };
var from = "johndoe#example.com";
var request = new SendRawEmailRequest
{
Destinations = to,
RawMessage = raw,
Source = from
};
sesClient.SendRawEmail(request);
I send invoices and warnings/reminders to customers and now these mails should be flagged with a Deliverynotification and Readnotification. So, I know when customers received and read the invoice.
MailMessage email = new MailMessage();
email.Priority = MailPriority.Normal;
email.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess; // Deliverynotification?
email.Attachments.Add(new Attachment(docDunPath));
email.Attachments.Add(new Attachment(docInvoicePath));
email.Subject = "-----------";
email.IsBodyHtml = true;
email.Body = MailText;
Everything works so far, I just want to set the options for an automatic response when the recipient has read the mail. So, I can be sure he saw his invoice.
So after realising i used System.Net.Mail i switched to Microsoft.Exchange.WebServices and it works perfectly now.
EmailMessage email = new EmailMessage(service);
email.IsDeliveryReceiptRequested = true;
email.IsReadReceiptRequested = true;
I'm using the following to send email from a site done in .net 4, c#.
MailMessage nMail = new MailMessage();
nMail.To.Add("new.address#test.com");
nMail.From = new MailAddress("me#me.com");
Mail.Subject = (" ");
nMail.Body = (" ");
SmtpClient sc = new SmtpClient("our server");
sc.Credentials = new System.Net.NetworkCredential("login", "pwd");
sc.Send(nMail);
Works fine, only thing I don't know how to do is format the body of the message to have multiple lines and include fields from the page itself.
Any pointers?
This is one way to make a message body with multiple lines.
var bodyBuilder = new StringBuilder();
bodyBuilder.AppendLine("First line.");
bodyBuilder.AppendLine("Second line.");
nMail.Body = bodyBuilder.ToString();
It should be obvious how to pull in values from your form now, too (i.e., the full power of string formatting is at your disposal now).
Follow this article. this will guide you through the way on how to format the mail body.
You can send message as HTML. Use IsBodyHtml property
MailMessage nMail = new MailMessage();
nMail.To.Add("new.address#test.com");
nMail.From = new MailAddress("me#me.com");
Mail.Subject = (" ");
nMail.Body = ("Line<br/>New line");
nMail.IsBodyHtml = true;
SmtpClient sc = new SmtpClient("our server");
sc.Credentials = new System.Net.NetworkCredential("login", "pwd");
sc.Send(nMail);
carefully look at this example, it should help how to pull in values from your form too (not the best method used though).
I've found this small code that sends email to gmail users. I'd like the body of the mail to contain html (for example, decoding a link for it to hold different text than the url it's pointing to).
I am using c# .net 3.5. I've used these classes in my code:
MailMessage
SmtpClient
How can this be done?
Here's a copy of my code:
MailMessage message = new MailMessage("me#gmail.com", WebCommon.UserEmail, "Test", context.Server.HtmlEncode("<html> <body> <a href='www.cnn.com'> test </a> </body> </html> "));
System.Net.NetworkCredential cred = new System.Net.NetworkCredential("him#gmail.com", "myPwd");
message.IsBodyHtml = true;
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("smtp.gmail.com");
smtp.UseDefaultCredentials = false;
smtp.EnableSsl = true;
smtp.Credentials = cred;
smtp.Port = 587;
smtp.Send(message);
Thanks!
Something like this should work:
Note that MailMessage refers to System.Net.MailMessage. There is also System.Web.MailMessage, which I have never used and -as far as I know- is obsolete.
MailMessage message = new MailMessage();
// Very basic html. HTML should always be valid, otherwise you go to spam
message.Body = "<html><body><p>test</p></body></html>";
// QuotedPrintable encoding is the default, but will often lead to trouble,
// so you should set something meaningful here. Could also be ASCII or some ISO
message.BodyEncoding = Encoding.UTF8;
message.IsBodyHtml = true;
// No Subject usually goes to spam, too
message.Subject = "Some Subject";
// Note that you can add multiple recipients, bcc, cc rec., etc. Using the
// address-only syntax, i.e. w/o a readable name saves you from some issues
message.To.Add("someone#gmail.com");
// SmtpHost, -Port, -User, -Password must be a valid account you can use to
// send messages. Note that it is very often required that the account you
// use also has the specified sender address associated!
// If you configure the Smtp yourself, you can change that of course
SmtpClient client = new SmtpClient(SmtpHost, SmtpPort) {
Credentials = new NetworkCredential(SmtpUser, SmtpPassword),
EnableSsl = enableSsl;
};
try {
// It might be necessary to enforce a specific sender address, see above
if (!string.IsNullOrEmpty(ForceSenderAddress)) {
message.From = new MailAddress(ForceSenderAddress);
}
client.Send(message);
}
catch (Exception ex) {
return false;
}
For more sophisticated templating solutions that render the Body html rather than hard-codin it, there is, for example, the EMailTemplateService in MvcContrib which you can use as a guideline.
One way to do it is to create an alternate html view of the email:
MailMessage message = new MailMessage();
message.Body = //plain-text version of message
//set up message...
//create html view
string htmlBody = "<html>...</html>";
htmlView = AlternateView.CreateAlternateViewFromString(htmlBody, null, "text/html");
message.AlternateViews.Add(htmlView);
//send message
smtpClient.Send(message);