format email body - .net 4.0 - c#

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).

Related

Add list-unsubscribe header with Amazon SES

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);

c# Send Email using Process.Start

I want to send simple email with no attachment using default email application.
I know it can be done using Process.Start, but I cannot get it to work. Here is what I have so far:
string mailto = string.Format("mailto:{0}?Subject={1}&Body={2}", "to#user.com", "Subject of message", "This is a body of a message");
System.Diagnostics.Process.Start(mailto);
But it simply opens Outlook message with pre-written text. I want to directly send this without having user to manually click "Send" button. What am I missing?
Thank you
You need to do this :
string mailto = string.Format("mailto:{0}?Subject={1}&Body={2}", "to#user.com", "Subject of message", "This is a body of a message");
mailto = Uri.EscapeUriString(mailto);
System.Diagnostics.Process.Start(mailto);
I'm not sure about Process.Start() this will probably always only open a mail message in the default Mail-App and not send it automatically.
But there may be two alternatives:
Send directly via SmtpClient Class
using Outlook.Interop
You need to do this:
SmtpClient m_objSmtpServer = new SmtpClient();
MailMessage objMail = new MailMessage();
m_objSmtpServer.Host = "YOURHOSTNAME";
m_objSmtpServer.Port = YOUR PORT NOS;
objMail.From = new MailAddress(fromaddress);
objMail.To.Add("TOADDRESS");
objMail.Subject = subject;
objMail.Body = description;
m_objSmtpServer.Send(objMail);

Mail body: & becomes (R)

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);

How to send email in richtext format to Outlook?

It works great to send emails (to Outlook) in HTML format by assigning the text/html content type string like so:
using (MailMessage message = new MailMessage())
{
message.From = new MailAddress("--#---.com");
message.ReplyTo = new MailAddress("--#---.com");
message.To.Add(new MailAddress("---#---.com"));
message.Subject = "This subject";
message.Body = "This content is in plain text";
message.IsBodyHtml = false;
string bodyHtml = "<p>This is the HTML <strong>content</strong>.</p>";
using (AlternateView altView = AlternateView.CreateAlternateViewFromString(bodyHtml,
new ContentType(MediaTypeNames.Text.Html)))
{
message.AlternateViews.Add(altView);
SmtpClient smtp = new SmtpClient(smtpAddress);
smtp.Send(message);
}
}
The email is correctly recognized as HTML in Outlook (2003).
But if I try rich text:
MediaTypeNames.RichText;
Outlook doesn't detect this, it falls back to plain text.
How do I send email in rich text format?
The bottom line is, you can't do this easily using System.Net.Mail.
The rich text in Outlook is sent as a winmail.dat file in the SMTP world (outside of Exchange).
The winmail.dat file is a TNEF message. So, you would need to create your richtext inside of the winmail.dat file (formatted to TNEF rules).
However, that's not all. Outlook uses a special version of compressed RTF, so, you would also need to compress your RTF down, before it's added to the winmail.dat file.
The bottom line, is this is difficult to do, and unless the client really, really needs this functionality, I would rethink it.
This isn't something you can do with a few lines of code in .NET.
You can also achieve this by adding another alternate view before your calendar view as below:
var body = AlternateView.CreateAlternateViewFromString(bodyHtml, new System.Net.Mime.ContentType("text/html"));
mailMessage.AlternateViews.Add(body);
This worked for me..
public void sendUsersMail(string recipientMailId, string ccMailList, string body, string subject)
{
try
{
MailMessage Msg = new MailMessage();
Msg.From = new MailAddress("norepl#xyz.com", "Tracker Tool");
Msg.To.Add(recipientMailId);
if (ccMailList != "")
Msg.CC.Add(ccMailList);
Msg.Subject = subject;
var AltBody = AlternateView.CreateAlternateViewFromString(body, new System.Net.Mime.ContentType("text/html"));
Msg.AlternateViews.Add(AltBody);
Msg.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient("mail.xyz.com");
smtp.Send(Msg);
smtp.Dispose();
}
catch (Exception ex)
{
}
}

send smtp mail including html to gmail account

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);

Categories

Resources