I need to send an email containing a string like this:
CSAclsEN\t001\t\\\\sgprt\\Projects2\t001\tCSAclsEN-2010-1140609\t10:03\t09JUN14\t2010\tNorth London branch\tGG02\t001\t****09\tCustomer 340509\t\r01\t******35 \tVariable rate Cash ISA\tGBP\t395.08\t\r02\t31MAR14\tInterest\t7.21+\t\t\r01\t******69 \tInstant Access account\tGBP\t0.00\t\r01\t******75 \t35 Day Notice account\tGBP\t412.11\t\r02\t27MAY14\tInterest\t0.22+\t\t\r02\t25APR14\tInterest\t0.21+\t\t\r02\t25MAR14\tInterest\t0.19+\t\t\r02\t25FEB14\tInterest\t0.21+\t\t\r02\t27JAN14\tInterest\t0.28+\t\t\r01\t******76 \t35 Day Notice account\tGBP\t0.00\t
(this is that it can be investigated in the same format it is taken off of the WebSpehere Message Queue.
Unfortunately the SMTP Mail seems to be taking that and turning it into this:
Here is my code:
SmtpClient emailSender;
MailMessage theMessage;
// Connect to the server.
emailSender = new SmtpClient("sgexc");
// Build the content details.
theMessage = new MailMessage();
theMessage.From = new MailAddress(emailFromAddress, "Cashier Printing System");
theMessage.Subject = emailSubject;
string[] toAddresses = emailToAddress.Split(new Char[] { ':' });
foreach (string toAddress in toAddresses)
{
theMessage.To.Add(toAddress);
}
string[] ccAddresses = emailCCAddress.Split(new Char[] { ':' });
//foreach (string ccAddress in ccAddresses)
// {
// theMessage.CC.Add(ccAddress);
// }
sMessage=sMessage.Replace(#"\","\\");
theMessage.Body = sMessage;
theMessage.IsBodyHtml = false;
// Fill in the details and send.
emailSender.Send(theMessage);
WHat do I need to do if I want to prevent Outlook or SMTP from parsing those escape sequences? I have tried replacing all the backslashes with 2 backslashes but no luck.
I found the answer, and it's very simple.
I just set the theMessage.IsBodyHtml = true;
Related
Currently my application has ability to process incoming emails and bounce then back if it does not match with given criteria in code. However, I want to add up another type of email to Process which are NDR "Non-Delivery Reports" from Microsoft Exchange Server. So my application do not responsd/Bounce back NDR to exchange server which cause a loop between my Mailbox and Exchange Server.
Following method triggers when Invalid doesn't have a specific
private static void ProcessInvidMsgWithoutNo(string sMsgFrom, string sFromEmail, EmailMsg sMsgReceived, EmailMessage message)
{
EmailMsg.MoveToInvalid(message);
sMsgReceived.IsValid = false;
SaveMsgReceived(sMsgReceived, 0, string.Empty);
if (!sFromEmail.Equals(string.Empty))
{
ResponseForInvidMsg(sFromEmail);
}
else
{
curLog.WriteLog(string.Format(CultureInfo.CurrentCulture, MsgEMChecker27, sMsgFrom));
}
}
Following Method triggers to respond incoming Invalid message as stated above.
private static void ResponseForInvidMsg(string sFromEmail)
{
string tErrSubjectMsg = String.Format(CultureInfo.CurrentCulture, "{0}\\Resource\\MsgErrorSubjectAck.html", Entity.GetSetting("DocRootDir"));
StringBuilder htmlText = new StringBuilder();
FileStream fsFile = new FileStream(tErrSubjectMsg, FileMode.Open);
if (fsFile != null)
{
StreamReader reader = new StreamReader(fsFile, Encoding.Default);
string text;
do
{
text = reader.ReadLine();
if ((text != null) && (text != ""))
htmlText.Append(text + "\n");
} while (text != null);
reader.Close();
fsFile.Close();
fsFile = null;
}
else
htmlText.Append("hello");
string tToCustomerSubject = ReplyForInvalid;
string tMessage = htmlText.ToString();
EmailMsg emTo = new EmailMsg(string.Empty, sFromEmail, tToCustomerSubject, tMessage);
emTo.MsgType = EmailMsg.TypeSentCustomer;
emTo.Send(false); //Not save but CC to generic email box
}
Please, help me to find a way where I can stop my code to respond Exchange Server NDR. Thanks
The place to start would be to check the ItemClass of the message see https://learn.microsoft.com/en-us/openspecs/exchange_server_protocols/ms-asemail/51d84da6-a2da-41e9-8ca7-eb6c4e72c28d . NDR's, delivery report etc should have a prefix of Report eg
REPORT.IPM.NOTE.NDR Non-delivery report for a standard message.
REPORT.IPM.NOTE.DR Delivery receipt for a standard message.
I'm using System.Net.Mail to send email in my application but I get an exception and I can't figure out what/where the problem is and how to fix it.
The error says I have some invalid char:
An invalid character was found in the mail header: ';'.
I tried google without success.
The string with email address is:
john#mydomain.org; beth#mydomain.org; alfred#mydomain.org; barbie#mydomain.org;
Here is my email sending code:
SmtpClient smtpClient = new SmtpClient("smtp.........");
System.Net.Mail.MailMessage mailMessagePlainText = new System.Net.Mail.MailMessage();
mailMessagePlainText.IsBodyHtml = true;
mailMessagePlainText.From = new MailAddress("vincent#mydomain.org", "admin");
mailMessagePlainText.Subject = "test";
mailMessagePlainText.Body = "test";
mailMessagePlainText.To.Add(new MailAddress(List1.ToString(), ""));
mailMessagePlainText.Bcc.Add(new MailAddress("vincent#mydomain.org", ""));
try
{
smtpClient.Send(mailMessagePlainText);
}
catch (Exception ex)
{
throw (ex);
}
foreach (var address in List1.split(';')) {
mailMessagePlainText.To.Add(new MailAddress(address.Trim(), ""));
}
Because according to your string here above, each address in this loop above would produce following:
"john#mydomain.org"
" beth#mydomain.org"
" alfred#mydomain.org"
" barbie#mydomain.org"
So by adding .Trim() to address would make your code work.
A MailAddressCollection (like your mailMessagePlainText.To) has an Add method that accepts a string containing a list of mail addresses, separated by a comma.
So to use that, you will need to change the ; into a , and possibly remove the extra spaces.
It looks like you're adding the addresses as a single MailAddress, where you need to add them 1 at a time. I don't know what other overloads are available, but the following will probably work.
I split the string by ; and add each address separately.
replace
mailMessagePlainText.To.Add(new MailAddress(List1.ToString(), ""));
with
foreach (var address in List1.split(';')) {
mailMessagePlainText.To.Add(new MailAddress(address , ""));
}
I'm generating a simple email using the System.Net.Mail.MailMessage class and building the body with a StringBuilder. I'm looping through a string[] and trying to append a new line each time. The problem I'm having is that I can't seem to generate a single new line each time. I can either get two or none.
What I've tried:
foreach (var message in messages)
{
body.AppendLine(message);
}
foreach (var message in messages)
{
body.Append(message + "\n");
}
foreach (var message in messages)
{
body.Append(message + System.Environment.NewLine);
}
I've also tried with string.Format().
For each example above, I get the same result. No new line being generated.
When I try the following, however, I get the expected result. A new line with an empty line in between.
foreach (var message in messages)
{
body.AppendLine(message + System.Environment.NewLine);
}
Why is it doing this and what can I do to just get a single new line each time?
Update:
So I've found that Outlook and probably Gmail (haven't tested others) are actually removing some line breaks and not others. Does anyone know why or how they determine what to remove?
When I checked the email in Outlook, I got a tiny info message saying "Extra line breaks in this message were removed" and the option to restore them. Gmail gave no such indication (that I found) but I must assume it removed them as well.
Interestingly enough I had line breaks elsewhere in the body that worked as expected, only the ones in the loop were deemed "extra".
I have modified my code to build the email body using html.
IsBodyHtml = true
If anyone knows of a way to prevent this, or what causes certain line breaks to be removed, please let me know.
Update:
Once I knew what I was looking for, I found this post which helps to explain things and gives some alternate solutions. I did not try any of them, as I think using html is the better solution in my case.
I have just ran this through LINQPad
string[] messages = new string[]{"First line in message", "Second Line in message", "Third Line in message"};
StringBuilder sbA = new StringBuilder();
StringBuilder sbB = new StringBuilder();
StringBuilder sbC = new StringBuilder();
StringBuilder sbD = new StringBuilder();
// New line for each string
foreach (var message in messages)
{
sbA.AppendLine(message);
}
// New line for each string
foreach (var message in messages)
{
sbB.Append(message + "\n");
}
// New line for each string
foreach (var message in messages)
{
sbC.Append(message + System.Environment.NewLine);
}
//One Line
foreach (var message in messages)
{
sbD.Append(message);
}
sbA.Dump();
sbB.Dump();
sbC.Dump();
sbD.Dump();
Each one performs as expected in a StringBuilder sense.
I suspect that you need to add "</br>" at the end of each AppendLine something like this (not tested):
MailMessage mail = new MailMessage(new MailAddress("someSender#email.com"), new MailAddress("someReceiver#email.com"));
string[] messages = new string[]{"First line in message", "Second Line in message", "Third Line in message"};
StringBuilder body = new StringBuilder();
body.AppendLine("<h2>My Message<h2></br>" );
foreach (var message in messages)
{
body.AppendLine(message + "</br>");
}
mail.Body = body.ToString();
mail.Dump();
Gmail will remove explicit line breaks. This code works for me when sending to Gmail, as well as Outlook with proper line breaks intact. I am completely unable to replicate your issue without seeing what your messages[] array looks like. It may be possible that some formatting could be viewed as line breaks causing all to be stripped out? I have tampered with my example strings and I cannot get Outlook nor Gmail to strip the line breaks on me.
private void SendMessage()
{
try
{
StringBuilder body = new StringBuilder();
string[] messages = { "test with a tab in it", " test with spaces", " \r\nTab, then line break", "\n\n\n\n\n\nlots of returns...", " test spaces" };
foreach (var msg in messages)
{
body.AppendLine(msg);
//body.AppendLine(string.Empty);
}
using (MailMessage message = new MailMessage())
{
message.From = new MailAddress("you#test.com", "Your Display Name");
message.ReplyToList.Add("reply-email#test.com");
message.To.Add("to#test.com");
message.Subject = "Test Line Break Message";
message.IsBodyHtml = false;
message.Priority = MailPriority.Normal;
message.BodyEncoding = System.Text.Encoding.UTF8;
message.Body = body.ToString();
using (SmtpClient smtpClient = new SmtpClient())
{
smtpClient.Host = "127.0.0.1";
smtpClient.Send(message);
}
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
I am going to assume somewhere in your code that builds or retrieves the messages array, something is happening to cause this issue. I would believe it to be fixable, but not without seeing how that part is coded.
I have a [WebMethod] Sendemail This works fine but now i what to upgrade it to send attachments. I am using a fileupload. This is my method Call.
lblEmailSent.Text = Send.Sendemail(txtTo.Text, txtSubject.Text, txtbody.Text, FileUpload1.PostedFile.FileName, FileUpload1.FileContent);
My Call Statement is underlined in blue and the two error given look like:
*1)*The best overloaded method match for 'WebTestServiceApp.localhost.Service1.Sendemail(string, string,
string, string, WebTestServiceApp.localhost.Stream)' has some invalid
arguments
*2)*Argument 5: cannot convert from 'System.IO.Stream' to 'WebTestServiceApp.localhost.Stream'
FileUpload1.PostedFile.FileName is passed as a String FileUpload1.FileContent is passed as a stream
This is my [WebMethod], Now u all can see every thing i can see, i can't spot anything wrong, But i am unsure if FileUpload1.FileContent should be passed as a Stream.
[WebMethod]
public string Sendemail(String inValueTo, String inValueSub, String inValueBody, String inValueAttachmentPostedfile, Stream inValueAttachemtnFileContent) //, String inValueAttachmentPostedfile, Stream inValueAttachemtnFileContent
{
try
{
String valueTo = inValueTo;
String valueSub = inValueSub;
String valueBody = inValueBody;
String valueAttachmentPostedfile = inValueAttachmentPostedfile; //FileUpload1.PostedFile.FileName
Stream valueAttachmentFileContent = inValueAttachemtnFileContent; //FileUpload1.FileContent.fileName
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(); // Creating new message.
message.To.Add(valueTo);
message.Subject = valueSub;
message.From = new System.Net.Mail.MailAddress("shaunmossop#mweb.co.za");
message.Body = valueBody;
message.IsBodyHtml = true;
string fileName = Path.GetFileName(valueAttachmentPostedfile); // Get attachment file
Attachment myAttachment =
new Attachment(valueAttachmentFileContent, fileName);
if (fileName != "")
{
message.Attachments.Add(myAttachment); // Send attachment
}
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("smtp.gmail.com"); //Properties.Settings.Default.MailSMTPServer
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = false;
NetworkCredential netC = new NetworkCredential(Properties.Settings.Default.username, Properties.Settings.Default.password); // Useing Projects defult settings.
smtp.Credentials = netC;
smtp.Send(message);
return "Message has been sent";
}
catch (Exception)
{
return "Message faild to send" ;
}
}
the second error it gives you hits the nail on the head, yes youre passing string string string string but youre passing the wrong type of stream for it to interact with, it wants this kind WebTestServiceApp.localhost.Stream
youre giving it this kind
System.IO.Stream
when you use the stream try explicitly declaring it as a WebTestServiceApp.localhost.Stream when you first make it, that way youre then passing the right types of stream and your problem should stop!
theres two types of stream you see, one used for web apps and one used for desktop apps.
I am using MailMessage class in my program. When the subject is too long subject will look like.
Subject:
=?utf-8?B?W0VudGVycHJpc2UgUHJpb3JpdHldIC0gQ3VzdG9tZXIgSW5jaWRlbnQgNjkxNzIgZm9yIEhhcmlkaGFyYW4gKDEzMjM5OSkgaGFyaWRoYXJhbnJAc3luY2Z1c2lvbi5jb20gOiBUZXN0aW5nIFRlc3RpbmcgVGVzdGluZyBUZXNpbmcgVGVzdGluZyBUZXN0aW5nIFRlc3RpbmcgVGVzdGluZyBUZXN0aW5nIFRlc3Rpbmcg4o"
This problem occurred in server only. while debugging i have used the same subject content in my "local" but, i got correct subject.
Program:
protected MailMessage msg;
msg.Subject = subject;
Got the same (error) subject in WebMail.IHostExchange.NET also.
What is the problem?
Update:
This is a part of my coding.
public EmailSenderThread(string emailAddresses, string ccemailaddress, string from, string subject, string body)
: base()
{
msgThread = new Thread(new ThreadStart(MailSender));
this.mailAddress = emailAddresses;
this.ccmailAddress = ccemailaddress;
msg.From = new MailAddress(from);
msg.IsBodyHtml = true;
msg.Body = body;
string[] mails = emailAddresses.Split(';');
foreach (string mail in mails)
if (!string.IsNullOrEmpty(mail))
msg.To.Add(mail);
if (ccemailaddress != string.Empty)
{
string[] ccemails = ccemailaddress.Split(';');
foreach (string ccmail in ccemails)
if (!string.IsNullOrEmpty(ccmail))
msg.CC.Add(ccmail);
}
msg.Subject = subject;
msgThread.Start();
}
I have already tried with
msg.SubjectEncoding = System.Text.Encoding.UTF8;
but i got the same error. Did you got my doubt. Please let me know if I didn't explain clearly.
1) why it is working fine in local? and why it is not working when i am hosting this into server. ?
2) What is the maximum length of the subject line?
2) What is the maximum length of the subject line?
From RFC822 about unstructured header fields:
Some field bodies in this standard are defined simply as "unstructured" (which is specified below as any US-ASCII characters, except for CR and LF) with no further restrictions. These are referred to as unstructured field bodies. Semantically, unstructured field bodies are simply to be treated as a single line of characters with no further processing (except for header "folding" and "unfolding" as described in section 2.2.3).
The subject line is an unstructured field, and therefore has no imposed length limit.
Without seeing more code, I'm going to guess an encoding problem - try specifying an encoding for your subject and body. Look at this post for sample code.