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);
Related
Sometimes following code sends Email,Sometimes i get SMTP Exception saying "failure in sending Email".Why is this happening? following is the code i have written.Plesae tell me why sometime i get email sent and sometime why i get Exception.
StringBuilder mailBody = new StringBuilder();
mailBody.Append("Hi <br/>")
MailMessage message = new MailMessage("from#gmail.com", "to#gmail.com");
message.Subject = "Subject";
message.Body = mailBody.ToString();
SmtpClient smtpclient = new SmtpClient("smtp.gmail.com", 587);
smtpclient.Credentials = new System.Net.NetworkCredential()
{ UserName = "from#gmail.com",
Password = "password"
};
smtpclient.EnableSsl = true;
message.IsBodyHtml = true;
smtpclient.Send(message);
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();
}
I have functionality in my site which sends a email to an id .. Email contains a link to a webpage and a security key .. But the problem is it goes into the junk folder
I'm using free hosting by Somee.com
Code:
MailMessage message = new MailMessage();
message.IsBodyHtml = true;
message.Body = ("Copy The Link And paste It In Them follow Link Download </br>"+ encoded_url);
message.From = new MailAddress("lz-wag#hotmail.com");
message.To.Add(TextBox2.Text);
message.Subject = user + " Has Share The File With You";
try{
SmtpClient client = new SmtpClient();
client.Host = "smtp.live.com";
client.EnableSsl = true;
System.Net.NetworkCredential networkcred = new System.Net.NetworkCredential();
networkcred.UserName = "lz-wag#hotmail.com";
networkcred.Password = "password";
client.Port = 587;
client.Credentials = networkcred;
client.Send(message);
sendFile.Visible = false;
Label1.Visible = true;
Label1.Text = "Your File Has Been Shared";
}
catch(Exception ex){
Label1.Visible = true;
Label1.Text = "Your File Is Not Shared";
//Label1.Text = ex.ToString(); ;
}
Whether or not the email goes into the junk mail folder is a function of the email client, not a function of how you are sending the email.
However, FYI, both the MailMessage and the SmtpClient implement IDisposable, so should be in using blocks. Something like this:
using (MailMessage message = new MailMessage())
{
// ...
using (SmtpClient client = new SmtpClient())
{
// ...
client.Send(message);
}
}
I also suggest that you log the exception somewhere, or you'll never know what went wrong when something goes wrong.
I am working on a project that has to send an email with an attachment. I have achieved sending mail, but I don't know how to attach an excel file.
Could someone point me in the right direction? Here's my code so far:
private void sendmail(string Tomailid, string name)
{
MailMessage mail = new MailMessage();
mail.From = new MailAddress("ehsimsemail#gmail.com");
mail.To.Add(Tomailid);
mail.Subject = "Test Mail";
StringBuilder sb = new StringBuilder();
sb.Append("Dear ");
sb.Append(string.Format("{0} ", name));
//sb.Append("Yuvaraj");
sb.Append(Environment.NewLine);
sb.Append(Environment.NewLine);
sb.Append("UCR No : ");
sb.Append("3256987");
sb.Append(" ");
sb.Append("HAS BEEN SENT FOR YOUR REVIEW AND APPROVAL");
sb.Append(Environment.NewLine);
sb.Append(Environment.NewLine);
sb.Append("For action, Please click Here");
sb.Append(Environment.NewLine);
sb.Append("This is a automatic generated report from Advanced Incident Management System (EHS-IMS). Please do not reply to this mail.");
sb.Append(Environment.NewLine);
sb.Append(Environment.NewLine);
sb.Append("Thanks & Regards,");
sb.Append(Environment.NewLine);
sb.Append("EHSIMS TEAM");
mail.Body = sb.ToString();
SmtpClient SmtpServer = new SmtpClient();
SmtpServer.Host = "smtp.gmail.com";
SmtpServer.Port = 587;
SmtpServer.EnableSsl = true;
SmtpServer.Credentials = new System.Net.NetworkCredential("ehsimsemail#gmail.com", "ehsims123");
SmtpServer.Send(mail);
}
That is so easy. So much material available on Internet.
Have a look at this link :
http://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.attachments.aspx
Short Code :
Attachment data = new Attachment(file, MediaTypeNames.Application.Octet);
// Add time stamp information for the file.
ContentDisposition disposition = data.ContentDisposition;
disposition.CreationDate = System.IO.File.GetCreationTime(file);
disposition.ModificationDate = System.IO.File.GetLastWriteTime(file);
disposition.ReadDate = System.IO.File.GetLastAccessTime(file);
// Add the file attachment to this e-mail message.
message.Attachments.Add(data);
You have to attach the file to your message.
mail.Attachments.Add(New Attachment(pathToExcelFile))
where pathToExcelFile is the file path to the file you want to send.
Make sure you have access rights to the file.
see the following for more information:
http://msdn.microsoft.com/en-us/library/system.net.mail.attachment.aspx
protected void btnSendMail_Click(object sender, EventArgs e)
{
string to = "reciever#gmail.com";
string from = "frommail#gmail.com";
string subject = "Mail Subject";
string body = "Mail Message";
MailMessage msgObj = new MailMessage();
msgObj.To.Add(to);
msgObj.Subject = subject;
msgObj.Body = body;
msgObj.IsBodyHtml = true;
msgObj.Attachments.Add(new Attachment(#"D:\sample.xlx"));
MailAddress objMail = new MailAddress(from, "emailCampaign", System.Text.Encoding.UTF8);
msgObj.From = objMail;
SmtpClient smtp = new SmtpClient();
smtp.Port = 587;
smtp.Host = "smtp.gmail.com";
smtp.Credentials = new System.Net.NetworkCredential(from, "password");
smtp.EnableSsl = true;
smtp.Send(msgObj);
}
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;