I am sending an html formatted email message using MailMessage class. Code is as follows:
MailMessage message = new MailMessage();
message.body = "<html><body><b>test message</b></body></html>";
message.IsBodyHtml = true;
....... skipped To/From settings - irrelevant .......
new SmtpClient().Send(message);
When I see this received message in Outlook, it shows the whole html:
<html>
<body>
<b>
test message
</b>
</body>
</html>
instead of just bolded text message.
What am I doing wrong?
1) If you're running Outlook 2003 on windows 7, there was a known issue when trying to render emails with "Undisclosed-recipients" set. A patch was released, can't seem to find it atm. Does this sound like it could be your problem?
2) Check your virus scanner is not causing the problem
3) Do HTML emails render correctly from other sources? If they aren't then perhaps something between the sender and the recipient (even if they are both you) might be causing the issue.
I actually fixed it by putting html version of the email into message.AlternateViews instead of message.Text
Related
I'm using Microsoft.Office.Interop.Outlook, VB.net and Office 2013 to generate a MailItem, and then send the item to Outlook, show the email window and let the user edit it/send it from Outlook 2013. The main things I'm doing are:
I create the Microsoft.Office.Interop.Outlook.MailItem object and fill it with the relevant information, I generate an HTML constant for the body like this
Private Const mstrHTML_FORMAT As String = "<html><p style='font-size:10pt;font-family:Arial;'>{0}</p></html>"
Then I add the text I want to a string variable strBody and use String.Format to insert the text in the HTMLBody of my object:
objMailItem.HTMLBody = String.Format(mstrHTML_FORMAT, strBody)
I also change the format of the body to HTML:
objMailItem.BodyFormat = Microsoft.Office.Interop.Outlook.OlBodyFormat.olFormatHTML
After a few other steps I send it to the view
objMailItem.Display(True)
My problem is, when the user sends the email, the receiver will see that the email has a message with the subject as Text
any clue of why this happens?
It's an Outlook "feature". Outlook purposely puts <end> in the message preview when the body isn't long enough to fill the preview.
It's not caused by your code or any bad HTML formatting.
I have a programm, interpreting the attachement of incoming mails and writing the results of my findings in the body of the received email.
No problem at all so far...the problem comes with mails that are signed. I am able to get the attachements of the signed mail by interpreting the .p7m-File that is attached, and writing into the body of the message like this:
emailMessage.Body += string.Format("</br></br>Erste Abweichung ({0} Fahrplan):</br>{1} - {2}",
kind, pos.FromTime.ToString("dd.MM.yyyy HH:mm:ss"),
pos.ToTime.ToString("dd.MM.yyyy HH:mm:ss"));
emailMessage.Update(ConflictResolutionMode.AutoResolve);
I can see that the body property is set in Visual Studio, but in Outlook I don't see any body text. It works great when the message is not signed.
The problem now is that I don't know if this is a problem with outlook, or if I somehow have to sign the body text that I have created.
Any hint would be appreciated, thanks!
For the signed/encrypted messages, the body is always extracted from the p7m attachment. PR_BODY, PR_HTML or PR_RTF_COMPRESSED are not used.
Think about it - the whole pointy of signing a message is to prevent anybody from tampering with its contents. That is precisely what you are trying to do.
You can of course turn the signed/encrypted message into a regular message by setting the MessageClass property to "IPM.Note" and extracting the data from the p7m file, but I doubt your users will appreciate that.
I'm sending an email from ASP.NET using MailMessage and SmtpClient. I want to introduce some line breaks but none of the following worked:
Adding tags sbBody.Append("<table width='100%'><tr><td></br></br>");
Adding \r\n sbBody.Append("<table width='100%'><tr><td>\r\n\r\n");
Appending a new line to the StringBuilder.
sbBody is a StringBuilder wich I use at the end to set MailMessage's body: mailMessage.Body = sbBody.ToString()
What Am I missing here? I'm viewing the emails in Outlook and off course I'm setting IsBodyHtml to true.
Thanks for your time.
EDIT: Solved, it was a syntax error, </br> instead of <br/>
Your br tags are wrong. They should be written as:
<br />
I have a plain text file that I need to read in using C#, manipulate it a bit then I need to email it. That's easy enough, but it also has to stay in the same format as it's original state:
This is an excerpt from a sample file "mySample.txt":
*****************************NB!!!!**********************************
*Please view http://www.sdfsdf.comsdfsdfsdf . *
*********************************************************************
*** DO NOT DELETE or ALTER ANY OF THE FOLLOWING TEXT ***
Company X PTY.
Lorem Ipsum Office
Last Change - 01 February 2008
APPLICATION TO ESTABLISH A COMMUNITY WITHIN
THE RESTIN DISTRICT OF THE IPSUM.
===================================================================
1. COMMUNITY and ACTION
Give the name of the community. This is the name that will be
used in tables and lists associating the community with the name
district and community forum. The community names that are
delegated by Lorem are at the district level
The Action field specifies whether this is a 'N'ew application, an
'U'pdate or a 'R' removal.
1a. Complete community name:**{0}**
1b. Action - [N]ew, [U]pdate, or [R]emoval :**{1}**
As you can see I've got place holders {0} and {1} in the file which is to be replaced by an automated process.
In my C# I'm using a stream reader to read the entire file into a StringBuilder object then replacing the place holders using the StringBuilder.AppendFormat method.
The problem is when I add the text to a email message body and send it the format ends up looking different. It looks like a bunch of spaces or tabs get removed in the process.
private void Submit_Click(object sender, EventArgs e)
{
//create mail client
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.To.Add(ConfigurationManager.AppSettings["SubmitToEmail"]);
message.Bcc.Add("xyz#test.com");
message.Subject = "Test Subject";
message.BodyEncoding = Encoding.ASCII;
message.IsBodyHtml = false;
message.Body = _PopulateForm(_GatherInput());//calls method to read the file and replace values
client.Send(message);
//cleanup
client = null;
message.Dispose();
message = null;
}
Anyone have any ideas on how to keep the formatting in tact?
Thanks,
Jacques
The problem you've got is that you're sending plain text as HTML. It's natural that the layout gets changed because HTML is displayed differently than text. Plain text has new lines (\n), tabs (\t), etc., while HTML has line breaks (<BR>) and different layout methods.
If I were you, I would 1st start out by replacing new lines with <BR> (there should be a replace function x.Replace("\n", "<BR>");)
As for the text items that are centered, wrap them in <p style="text-align:center" }>, </p>.
The answer seems to have been with .net and the Mail message object. Instead of adding the content to the Body property, you have to create an alternateView object and add it to that. That solved my problem.
Regards,
Jacques
I had an interesting bug today when sending plain text emails from our system. We format messages like this:
-1st something happened blab bla.
-2nd something else happened blab blab blaa bla.
Today we had an email that looked like this:
-1st something happened blab bla.
-2nd something else happened blab blab blaa bla $1000.00 -3rd Something happened.
So above, we see that we lost the CrLf but only on the message that didn't have a period and ended in 0. I went through the code and found the CrLf is intack up until we send the email. I tracked the code down below, I am guessing it applies to C# as well:
NOTE: We use Environment.NewLine when building the string for the body.
Building the string:
If Not errorList Is Nothing Then
If errorList.Count > 0 Then
strBldrBody.Append(EMailHelper.CrLf)
strBldrBody.Append(EMailHelper.CrLf)
strBldrBody.Append("Response Error List:")
For Each itm As String In errorList
strBldrBody.Append(EMailHelper.CrLf)
strBldrBody.Append(DataHelper.DASH)
strBldrBody.Append(itm)
Next
End If
End If
Email encoding setting:
Try
If Not String.IsNullOrEmpty(recipient) Then
Using mailMsg As MailMessage = New MailMessage()
mailMsg.From = New MailAddress(_configHelper.EmailFrom)
mailMsg.Subject = subject
mailMsg.Body = body
mailMsg.BodyEncoding = Encoding.UTF8
EMailHelper.SetToAddress(recipient, mailMsg)
Dim smtpClient As SmtpClient = New SmtpClient(_configHelper.EmailRelayServer)
smtpClient.Send(mailMsg)
End Using
End If
Catch ex As System.Exception
'logs error
End Try
I want to know what happened in the string translation during the UTF-8 encoding/decoding that removes the CrLf?!
The problem was Outlook, see below:
outlook screen shot
We just had a problem similar to this, but it turned out to be a conversion we did from a bytestream to a string. I have also seen this happen when getting encodings mixed up. From the code posted, I don't see how this could happen.
Oh, and to Hans - he meant 'intact' not 'in tack'
You say that the message is intact (including that newline) up to the point where you send the message, but when the message was received it didn't contain the newline? Can you duplicate this error?
Whereas it's possible that the SmtpClient control somehow lost the newline, it's also possible that one of the relay servers lost it in translation somewhere, or that your email client didn't render it correctly.
Messages go through an incredible number of different translation steps between servers, and some of those translations are ... less than rigorously debugged. In particular, the quoted-printable encoding has some interesting edge cases that lots of implementations don't get correct.
If your relay server is local, I would suggest turning on logging so that it will save a copy of the message as it's received from you. And on the receiving end, get the actual bits of the message (if possible), without any kind of translation by client software.