How to send mail by EAsendmail in asp.net? - asp.net

I’m sending mail in asp like this:
MailMessage msg = new MailMessage();
msg.From = new MailAddress("from#email", "From Name");
msg.Subject = "Subject";
msg.To.Add("to#email");
msg.BodyEncoding = Encoding.UTF8;
String plainBody = "Body of plain email";
AlternateView plainView = AlternateView.CreateAlternateViewFromString(plainBody, Encoding.UTF8, "text/plain");
msg.AlternateViews.Add(plainView);
//now create the HTML version
MailDefinition message = new MailDefinition();
message.BodyFileName = "~/MailTemplates/template1.htm";
message.IsBodyHtml = true;
message.From = "from#email";
message.Subject = "Subject";
ListDictionary replacements = new ListDictionary();
replacements.Add("<%USERNAME%>", "ToUsername");//example of dynamic content for Username
MailMessage msgHtml = message.CreateMailMessage("to#email", replacements, new LiteralControl ());
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(msgHtml.Body, Encoding.UTF8, "text/html");
LinkedResource imgRes = new LinkedResource(Server.MapPath("~/MailTemplates/images/imgA.jpg"), System.Net.Mime.MediaTypeNames.Image.Jpeg);
imgRes.ContentId = "imgA";
imgRes.ContentType.Name = "imgA.jpg";
imgRes.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;
htmlView.LinkedResources.Add(imgRes);
msg.AlternateViews.Add(htmlView);
//sending prepared email
SmtpClient smtp = new SmtpClient();//It reads the SMPT params from Web.config
smtp.Send(msg);
It's ok.
I should change mail format and but how I add html file in new format??
New format:
SmtpClient oSmtp = new SmtpClient();
SmtpMail oMail = new SmtpMail("TryIt"); //should be TryIt
SmtpServer oServer = new SmtpServer("mail.ir");
oServer.Port = 465;
oServer.ConnectType = SmtpConnectType.ConnectSSLAuto;
oMail.From = "";
oServer.User = "...#...";
oServer.Password = "123";
oMail.To = "#";
oMail.Subject = ;
oMail.HtmlBody =;
oSmtp.SendMail(oServer, oMail);
I don’t want use html inside code, I’d like design a beautiful html file and send it.

You can read the HTML file using the following line of code...and then pass text to your email object.
using system.IO;
string text = System.IO.File.ReadAllText(#"C:\Users\Public\TestFolder\WriteText.html");
oMail.HtmlBody = text;

Related

Sending Mail using dynamic values with asp.net core 2.0

I'm sending email via asp.net Core 2.0 like this tutorial, so I have something like this into my controller
SmtpClient client = new SmtpClient("mysmtpserver");
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("username", "password");
MailMessage mailMessage = new MailMessage();
mailMessage.From = new MailAddress("whoever#me.com");
mailMessage.To.Add("receiver#me.com");
mailMessage.Body = "body";
mailMessage.Subject = "subject";
client.Send(mailMessage);
and it works, but I want to do it less generic. Like sending code to class and call it from controller. For example in class I want to use variables instead static content like
mailMessage.Body = "body";
Instead this I want to use something like:
var body;
mailMessage.Body = body;
So into controller have ability to change that content. How can I achieve that? Regards
In your controller, add a private function called SendEmail something like
private bool SendEmail(string mail_to, string mail_subject, mail_body)
{
bool result = false;
try
{
SmtpClient client = new SmtpClient("mysmtpserver");
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("username", "password");
MailMessage mailMessage = new MailMessage();
mailMessage.From = new MailAddress("whoever#me.com");
mailMessage.To.Add(mail_to);
mailMessage.Body = mail_body;
mailMessage.Subject = mail_subject;
client.Send(mailMessage);
result = true;
}
catch(Exception ex){ result = false; }
return result;
}
Use it in your controller
string mailBody = "Anything can be in the body\n. Mail contents.";
string subject = "Mail Subject";
string mailTo = "someone#someone.com"
SendEmail(mailTo, subject, mailBody)

How to get Dynamic Data in email newsletter from the website in asp.net c#?

I have a newsletter design in which i have to bind data dynamically and send mail to users in asp.net c# through noreply#xyz.com
I tried but getting error of authentication if anyone knows please sort out the problem.
MailMessage Msg = new MailMessage();
MailAddress fromMail = new MailAddress("abc#gmail.com");
// Sender e-mail address.
Msg.From = fromMail;
// Recipient e-mail address.
Msg.To.Add(new MailAddress("xyz#gmail.com"));
// Subject of e-mail
Msg.Subject = "Send Gridivew in EMail";
Msg.Body += "Please check below data <br/><br/>";
Msg.Body += GetGridviewData(gvUserInfo);
Msg.IsBodyHtml = true;
string sSmtpServer = "";
sSmtpServer = "smtp.gmail.com";
SmtpClient a = new SmtpClient();
a.Host = sSmtpServer;
a.EnableSsl = true;
a.Port = 587;
a.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
a.Credentials = new NetworkCredential("abc#gmail.com", "abc#123");
a.Send(Msg);
Grid View Code
public string GetGridviewData(GridView gv)
{
StringBuilder strBuilder = new StringBuilder();
StringWriter strWriter = new StringWriter(strBuilder);
HtmlTextWriter htw = new HtmlTextWriter(strWriter);
gv.RenderControl(htw);
return strBuilder.ToString();
}

ASP.Net : Send email with Images embedded in Rich Text HTML body

Sending a mail along with embedded image & HTML Text using asp.net.
I have tried like this:
public ActionResult Contact(tblCredential data)
{
string emailAddress = data.UserName;
string password = data.Password;
if (!string.IsNullOrEmpty(emailAddress))
{
//Send email to consultancy
string htmlText = "<img src='R:/MVC#2/EmailWithHtmlBody/EmailWithHtmlBody/images/message.jpg'></img>
<h1>Thank you</h1>";
string from = "******#gmail.com"; // Your Mail-id here emailAddress
string #touser = emailAddress; // To Mail-id
MailMessage mail = new MailMessage(from, touser);
{
mail.Subject = emailAddress + " sent a message";
mail.Body = htmlText;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient { Host = "smtp.gmail.com", EnableSsl = true };
NetworkCredential networkCredential = new NetworkCredential(from, "*****"); //Your Password here..
smtp.UseDefaultCredentials = false;
smtp.Credentials = networkCredential;
smtp.Port = 587;
smtp.Send(mail);
}
}
return RedirectToAction("Index");
}
Email is sent but HTML code is not working. In the mail it is showing HTML tags.
Help me.
In your code you are setting mail.IsBodyHtml = true; first, then mail.IsBodyHtml = false; again. Obviously, this won't work.
Btw: You cannot embed an image using the local path. The recipient will not have your image on his local machine. Embed it using inline embedding (Base64 Encoding) like it is shown here: https://sendgrid.com/blog/embedding-images-emails-facts/
<img alt="My Image" src="data:image/jpeg;base64,/9j/4S/+RXhpZgAATU0AKgAAAAgACAESAAMAENkDZ5u8/61a+X...more encoding" />
Try this template.
It helps to use smtp.port=25
try
{
MailMessage msg = new MailMessage ();
MailAddress fromAdd = new MailAddress("fromemail#email.com");
msg.[To].Add("toemail#email.com");
msg.Subject = "Choose Session Members";
msg.From = fromAdd;
msg .IsBodyHtml = true;
msg.Priority = MailPriority.Normal;
msg .BodyEncoding = Encoding.Default;
msg.Body = "<center><table><tr><td><h1>Your Message</h1><br/><br/></td></tr>";
msg.Body = msg.Body + "</table></center>";
SmtpClient smtpClient = new SmtpClient ("smtp.yourserver.com", "25");
smtpClient.EnableSsl = true;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new System.Net.NetworkCredential("yourname#yourserver.com", "password");
smtpClient .DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.Send(msg);
smtpClient.Dispose();
}

Mail excel attachment in asp.net C#

I am trying to attach a excel file with mail, but not getting where I am doing the mistake. There is no error coming but mail does not get send. When I try to check the server path of file with file exist. it show me true, so my file path is correct.
Mail is going properly without attachment.
MailMessage Msg = new MailMessage();
Msg.Subject = "User Creation";
string body = MailSending.PrepareMailBodyWith("WelcomeTemplate.htm", "LoginID", LoginID, "password", password);
string clientCoverage = System.Web.HttpContext.Current.Server.MapPath("~/file/abc.xlsx");
bool isFile = false;
if (File.Exists(clientCoverage))
{
isFile = true;
}
byte[] bytes = System.IO.File.ReadAllBytes(clientCoverage);
MemoryStream stream = new MemoryStream(bytes);
lblerror.Text = avnTutorial + isFile.ToString() ;
Attachment attachment = new Attachment(stream, "document.xlsx");
Msg.Attachments.Add(attachment);
Msg.Body = body;
Msg.IsBodyHtml = true;
Msg.From = new MailAddress("info#xyz.in");
Msg.To.Add(new MailAddress(Email));
SmtpClient client = new SmtpClient();
client.Host = "mail.xyz.in";
client.Port = 25;
client.Credentials = new System.Net.NetworkCredential("info#xyz.in", "xyz");
client.Send(Msg);
I tried with without memory stream also here is the code but does not work.
Attachment attachment = new Attachment(clientCoverage, MediaTypeNames.Application.Octet);

i am sending from asp.net with out any errors...but the html template is not viewing in mail..it showing download option only

Actually the mail(html template) is sending from asp.net with out any errors...but the html template is not viewing in mail..it showing download option only..
code is ..
string strMailContent = "Welcome new user";
string cid = "banner.jpg";
string path = Server.MapPath(#"images/banner.jpg"); // my logo is placed in images folder
MailMessage mailMessage = new MailMessage();
MailAddressCollection addcoll = new MailAddressCollection();
addcoll.Add("kollihari1209#gmail.com");
addcoll.Add("kollihari1209#gmail.com");
mailMessage.From = addcoll[0];
mailMessage.To.Add(addcoll[1]);
mailMessage.Subject = "Welcome new User";
LinkedResource logo = new LinkedResource(path);
logo.ContentId = "banner";
AlternateView av1 = AlternateView.CreateAlternateViewFromString("<html><body><img src=cid:companylogo/> <br></body></html>" + strMailContent, null, MediaTypeNames.Text.Html);
av1.LinkedResources.Add(logo);
mailMessage.AlternateViews.Add(av1);
mailMessage.IsBodyHtml = true;
SmtpClient mailSender = new SmtpClient();
mailSender.Host = "smtp.gmail.com";
mailSender.Port = 587;
You have added your text after the end of the html body tag. It needs to be inside of the body.

Resources