I am trying to make new Lines using a Web service (SOAP in asmx) Rather than have it send the Email with new Lines using "\n", it just shows a straight Line.
My code looks like this :
[WebMethod]
public bool SendCashBagSerial(string email, string fullname, string SerialNumCode, string purpleworksemail)
{
string to = email; //To address
string from = purpleworksemail;
MailMessage message = new MailMessage(from, to);
string EmailBody = "Dear "+ fullname +"\n Your CashBag Serial is : "+ SerialNumCode + "\n Please Quote this Number to Complete your Request. \n Regards, \n Purpleworks \n ";
message.Subject = "Your Cash Bag Serial!";
message.Body = EmailBody;
message.BodyEncoding = Encoding.UTF8;
message.IsBodyHtml = true;
SmtpClient client = new SmtpClient("smtp.office365.com", 587);
System.Net.NetworkCredential basicCredential1 = new System.Net.NetworkCredential("*******#*************", "*******");
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = basicCredential1;
try
{
client.Send(message);
}
catch(Exception ex)
{
throw ex;
}
return true;
}
Is there something I am not doing correctly?
Line breaks are often 2 characters, a line feed and a carriage return. To replicate this you do \r\n... but your email is html
message.IsBodyHtml = true;
So you need to use <br>
Related
I am sending emails thorugh c#. The body of the mail is dynamic i.e. user fills it in before sending the mail. Issue is the body text is coming in a single line instead of multiple lines as they should.
using (MailMessage mm = new MailMessage(txtEmail.Text.Trim(), email.Trim()))
{
try
{
mm.CC.Add(txtcc.Text.Trim());
mm.Subject = txtSubject.Text.Trim();
mm.Body = txtBody.Text;
MemoryStream pdf = new MemoryStream(bytes);
mm.Attachments.Add(new Attachment(pdf, "Report.pdf"));
mm.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
NetworkCredential credentials = new NetworkCredential();
credentials.UserName = txtEmail.Text.Trim();
credentials.Password = txtPassword.Text.Trim();
smtp.UseDefaultCredentials = true;
smtp.Credentials = credentials;
smtp.Port = 587;
smtp.Send(mm);
}
catch (Exception ex)
{
Alert.show1("Could not send the e-mail - error: " + ex.Message, this);
return;
}
}
If you dont need the potential of HTML formatting, you could avoid the problem changing the email format to plain text. This could also avoid user entering text that could be parsed as html.
mm.IsBodyHtml = false;
Otherwise you should change the end line character to html breaking line:
mm.IsBodyHtml = true;
mm.Body = mm.Body.Replace(#"\n", "<br />");
In case of the html format option, It would be wise to follow Fildor's comment and add an alternateView in plain text for email clients that are not html capable.
I have a HTML page (using div tags) How to put body as html tags?
same as i just want to mail same format Receipt
code
using System.IO;
using System.Net;
using System.Net.Mail;
string to = " "; //To address
string from = ""; //From address
MailMessage message = new MailMessage(from, to);
string mailbody = "In this article you will learn how to send a email using Asp.Net & C#";
message.Subject = "Sending Email Using Asp.Net & C#";
message.Body = mailbody;
message.BodyEncoding = Encoding.UTF8;
message.IsBodyHtml = true;
SmtpClient client = new SmtpClient("smtp.gmail.com", 587); //Gmail smtp
System.Net.NetworkCredential basicCredential1 = new
System.Net.NetworkCredential("yourmail id", "Password");
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = basicCredential1;
try
{
client.Send(message);
}
catch (Exception ex)
{
throw ex;
}
message.IsBodyHtml = true; is already set to true, so you have just to replace your mailbody by a html formatted text, something like this :
string mailbody = "<html><body>In this article you will learn how to send a email using Asp.Net & C#</body></html>";
I'm creating a contact page for visitors of my web site to send me email (in C#). When trying, I got an error about EHLO argument(s). Here is my code:
I got the error:
The server response was: Syntactically invalid EHLO argument(s).
Please, help
try
{
MailMessage _email = new MailMessage();
String MessageString = "";
MessageString = "<br>";
MessageString += "<b>Message from " + champNom.Value + "</b><br /><br />";
MessageString += "<br/><br/>";
MessageString += champMail.Text;
_email.Subject = "Mail from " + champNom.Value + " (Email adress: " + champEmail.Text + ")";
_email.From = new System.Net.Mail.MailAddress(System.Configuration.ConfigurationManager.AppSettings["SenderForEmail"].ToString(), "MyName");
_email.To.Add(new System.Net.Mail.MailAddress(System.Configuration.ConfigurationManager.AppSettings["AdminForEmail"].ToString(), "MyName"));
if (chkCCMyself.Checked) {
_email.CC.Add(new System.Net.Mail.MailAddress(champEmail.Text, champNom.Value));
}
_email.Body = MessageString;
_email.IsBodyHtml = true;
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();
smtp.EnableSsl = false;
//smtp.UseDefaultCredentials = true;
//smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Host = System.Configuration.ConfigurationManager.AppSettings["HostForEmail"].ToString();
smtp.Port = 587;
smtp.Credentials = new System.Net.NetworkCredential(System.Configuration.ConfigurationManager.AppSettings["SenderForEmail"].ToString(), System.Configuration.ConfigurationManager.AppSettings["SenderPswdForEmail"].ToString());
smtp.Send(_email);
Page.RegisterStartupScript("UserMsg", "<script>alert('Mail envoyé avec succès...');if(alert){ window.location='contact.aspx';}</script>");
}catch(Exception err){
champMail.Text += Environment.NewLine + err.Message;
}
Thanks in advance
As documented here http://forums.asp.net/t/1622198.aspx?Syntactically+invalid+EHLO+argument+s+emailing
If you email having problem with rejected EHLO or HELO, caused by syntactically invalid argument(s).
1.Possible the hostname contains underscore(_).
2.This is not standard practice, having _ for your domain
3.for example takizo_mail.takizo.com
4.Most of the mail server rejected hostname with underscore.
5.Possibility is a Windows Admin
Make sure the server-name has only latin-characters in it, and no special symbols like underscore etc.
MailMessage msg = new MailMessage("from#a.com", "To#y.com");
msg.Subject = "Intraday";
msg.Sender = new MailAddress("sender#y.com");
msg.IsBodyHtml = false; //to preserve line breaks and to avoid the need for <br>s
msg.Body = "Body";
SmtpClient client = new SmtpClient("Server");
client.Send(msg);
i am sending single mail to more than 1 person. so i am sending mail in for loop with attachement.but while in second loop i am getting file lock error.below is my code.
public string SendMail(string toList, string from, string ccList,
string subject, string body)
{
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
SmtpClient smtpClient = new SmtpClient();
string msg = string.Empty;
try
{
MailAddress fromAddress = new MailAddress(from);
message.From = fromAddress;
message.To.Add(toList);
if (ccList != null && ccList != string.Empty)
message.CC.Add(ccList);
message.Subject = subject;
message.IsBodyHtml = true;
message.Body = body;
// We use gmail as our smtp client
smtpClient.Host = "smtp.gmail.com";
smtpClient.Port = 587;
smtpClient.EnableSsl = true;
smtpClient.UseDefaultCredentials = true;
smtpClient.Credentials = new System.Net.NetworkCredential(TextBox1.Text, TextBox2.Text);
if (FileUpload2.HasFile)
{
// File Upload path
String FileName = FileUpload2.PostedFile.FileName;
FileUpload2.PostedFile.SaveAs(Server.MapPath(FileName));
//Getting Attachment file
Attachment mailAttachment = new Attachment(Server.MapPath(FileName));
//Attaching uploaded file
message.Attachments.Add(mailAttachment);
}
smtpClient.Send(message);
LblMessage.ForeColor = System.Drawing.Color.Green;
LblMessage.Text = "Mail Sent Successfully.";
}
catch (Exception ex)
{
LblMessage.Text = ex.Message;
LblMessage.ForeColor = System.Drawing.Color.Red;
}
return msg;
}
in loop calling this function
for (int i = 0; i < LbEmails.Items.Count; i++)
{
SendMail(LbEmails.Items[i].ToString(), TextBox1.Text, "", TbSubject.Text, TbBody.Text);
}
It looks like you're saving the uploaded file once for each email you send even though it seems to be the same for all the emails. Pull this line out of the SendMail() method and call it before your loop:
FileUpload2.PostedFile.SaveAs(Server.MapPath(FileName));
I am trying to send a basic email to a folderbut howerver, even though I do receive the email, the body is missing completely.
private void MailReport()
{
string to = "arianul#gmail.com";
string From = "ArianG#lr.co.za";
string subject = "Report";
**string Body = "Dear sir ,<br> Plz Check d Attachment <br><br>";**
bool send = sendMail(to, From, subject, Body);
if (send == true)
{
string CloseWindow = "alert('Mail Sent Successfully!');";
ClientScript.RegisterStartupScript(this.GetType(), "CloseWindow", CloseWindow, true);
}
else
{
string CloseWindow = "alert('Problem in Sending mail...try later!');";
ClientScript.RegisterStartupScript(this.GetType(), "CloseWindow", CloseWindow, true);
}
}
public bool sendMail(string to, string from, string subject, string body)
{
bool k = false;
try
{
MailMessage msg = new MailMessage(from, to);
msg.Subject = subject;
AlternateView view;
SmtpClient client;
StringBuilder msgText = new StringBuilder();
view = AlternateView.CreateAlternateViewFromString(msgText.ToString(), null, "text/html");
msg.AlternateViews.Add(view);
msgText.Append("<body><br></body></html> <br><br><br> " + body);
client = new SmtpClient();
client.Host = "staging.itmaniax.co.za";
client.Send(msg);
k = true;
}
catch (Exception exe)
{
Console.WriteLine(exe.ToString());
}
return k;
}
<system.net>
<mailSettings >
<smtp deliveryMethod="specifiedPickupDirectory" from="ArianG#lr.co.za">
<network host="staging.itmaniax.co.za"/>
<specifiedPickupDirectory pickupDirectoryLocation="C:\testdump\emaildump\"/>
</smtp>
</mailSettings>
I have the feeling the problem lies with the body and the html as it is done with visual studio-2008.
Any advice perhaps?
You can try something like below.
protected void SendMail()
{
// Gmail Address from where you send the mail
var fromAddress = "Gmail#gmail.com";
// any address where the email will be sending
var toAddress = YourEmail.Text.ToString();
//Password of your gmail address
const string fromPassword = "Password";
// Passing the values and make a email formate to display
string subject = YourSubject.Text.ToString();
string body = "From: " + YourName.Text + "\n";
body += "Email: " + YourEmail.Text + "\n";
body += "Subject: " + YourSubject.Text + "\n";
body += "Question: \n" + Comments.Text + "\n";
// smtp settings
var smtp = new System.Net.Mail.SmtpClient();
{
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
smtp.Timeout = 20000;
}
// Passing values to smtp object
smtp.Send(fromAddress, toAddress, subject, body);
}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
//here on button click what will done
SendMail();
DisplayMessage.Text = "Your Comments after sending the mail";
DisplayMessage.Visible = true;
YourSubject.Text = "";
YourEmail.Text = "";
YourName.Text = "";
Comments.Text = "";
}
catch (Exception) { }
}
For more information check Send Mail / Contact Form using ASP.NET and C#
I hope this will help to you.
it seems that you are missing the:
MailMessage msg = new MailMessage(from, to);
msg.Subject = subject;
msg.Body = body; <--- try adding this and see what happens.
msg.Body property is empty in your example, I guess that's why it doesn't send the body text.