How to design SMTP mail content in c# - c#

Software that I'm developing right now sends e-mail to responsible when a problem occurs in a production line. However while doing this email,I prefer it with a design like a card not a normal string.
I have just a little knowledge HTML and tried it but result was terrible.
So is there any API for c# to make this?

Look at this sample. May be it can help you.
MailAddress addressFrom = new MailAddress("jack.du#e-iceblue.com", "Jack Du");
MailAddress addressTo = new MailAddress("susanwong32#outlook.com");
MailMessage message = new MailMessage(addressFrom, addressTo);
message.Date = DateTime.Now;
message.Subject = "Sending Email with HTML Body";
string htmlString = #"<html>
<body>
<p>Dear Ms. Susan,</p>
<p>Thank you for your letter of yesterday inviting me to come for
an interview on Friday afternoon, 5th July, at 2:30.I shall be happy to be there as
requested and will bring my diploma and other papers with me.</p>
<p>Sincerely,<br>-Jack</br></p>
</body>
</html>
";
message.BodyHtml = htmlString;
SmtpClient client= new SmtpClient();
client.Host = "smtp.outlook.com";
client.Port = 587;
client.Username = addressFrom.Address;
client.Password = "password";
client.ConnectionProtocols = ConnectionProtocols.Ssl;
client.SendOne(message);
Console.WriteLine("Sent Successfully!");
Console.Read();

You should use the below property in order to get your content in the Html format.
message.IsBodyHtml = true;
Hope this is helpful

Related

How to send email from a C# program

I would like to add email functionality to a WinForm program I'm writing in C#. I have an Android app that has email functionality. What it does is set up the email but then lets the user choose the email program, etc. Once that is chosen the email body is completed. But it's up to the use to select what email app they want to use.
I would like to do the same in Windows but I don't see how. I have tried the following (based on other questions and responses here) :
_from = new MailAddress("my email address", "xxxx");
_to = new MailAddress("xxxx3333#gmail.com", "yyyy");
SmtpClient smtp = new SmtpClient("smtp.gmail.com");
smtp.UseDefaultCredentials = true;
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp msgMail = new MailMessage();
smtp.Body = text;
msgMail.Subject = "Subject";
msgMail.From = _from;
msgMail.To.Add(_to);
smtp.EnableSsl = true;
msgMail.Subject = _subject;
msgMail.Body = Text;
msgMail.IsBodyHtml = false;
try
{
mailClient.Send(msgMail);
}
catch (Exception ex)
{
string msg = "Exception caught in sending the email: " + ex.ToString();
showMessage(msg);
}
msgMail.Dispose();
But I get:
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.
With similar code in Android, my program just gets to an email form but lets the user decide what email add they will use.
Is there a way to do this in Windows?
There is an almost identical question and response here:
C# Windows Form Application - Send email using gmail smtp
And I think I've followed this but...doesn't work.
To directly answer your question - you probably haven't enabled less secure apps on the gmail account you are using.
Otherwise though, you could investigate the syntax of mailto if you want to let the user elect a mail client to use to send the email: https://www.labnol.org/internet/email/learn-mailto-syntax/6748/
From the link:
Send an email to Barack Obama with the subject “Congrats Obama” and some text in the body of the email message
<a href=”mailto:obama#whitehouse.gov?
subject=Congrats%20Obama&body=Enjoy%20your%20stay%0ARegards%20″>
This isn't directly related to C#/Windows - but I do know entering mailto:someone#somewhere.com at the Run prompt works:
Presumably then you could do something like: (untested)
Process.Run("mailto:someone#somewhere.com");
From the server response messages it looks like you have to provide login credentials before you are allowed to send.
Replace:
smtp.UseDefaultCredentials = true;
With:
smtp.Credentials = new System.Net.NetworkCredential("yourusername", "yourpassword");
This should do the trick.
You may have forgotten in your code to add the Host
Try to use this :
SmtpClient smtp = new SmtpClient();
smtp.UseDefaultCredentials = true;
smtp.Host = "SRVMAIL";

Can't send mail via c# using monodevelop on linux mint

I have a function in a large c# project that receives an email and a confirmation code and needs to send an email to that address:
public int sendVerificationEmail (string email, int randomNumber)
{
try
{
SmtpClient client = new SmtpClient ();
client.Port = 587;
client.Host = "smtp.gmail.com";
client.EnableSsl = true;
client.Timeout = 10000;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential ("wsep142#gmail.com", "<PASSWORD>");
MailMessage mm = new MailMessage ("wsep142#gmail.com", email, "Registration to the forum system", "This is your authentication code: " + randomNumber.ToString () + ".\n Please enter this link: www.myawesomewebsite.com and insert the code. \n Thank you and have a great day. \n WSEP142 Forum team.");
mm.BodyEncoding = UTF8Encoding.UTF8;
mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
client.Send (mm);
return 0;
}
catch(Exception)
{
return -1;
}
}
This code, using windows sends the mail correctly and everything, but when I try using it on linux (mint) using monodeveloper it stucks and can't send anything..
It doesn't get an exception or anything, just get stucked.
What to do?
If things work on Windows and not in Mint it's not your code that's wrong. It's something within the Mint environment. Perhaps a firewall setting blocking outbound traffic on port 25??
As an aside you could always try SendGrid to send the email. They have a number of libs that are easy to use. I tend to use this API now whenever I need to send external emails.
Regards
Steve.

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

format email body - .net 4.0

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

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