This is my mail sending code. I was getting "There is Invalid character in Mail Header" error.When i changed my Computer Name some shortest name. The problem solved. But in my domain whole computer names like "04500-ab04545.xxxdomain.gov.tr" so I need to find another solution for this problem.
So I cant give a static computer name while sending mail from c# code.
MailMessage msg = new MailMessage();
msg.Body = "axxxxxx";
msg.To.Add(new MailAddress("xxxx#xxxx.domain"));
msg.From = new MailAddress("xxxx#xxxx.domain","blab blalb");
msg.Subject = "Subject xxx";
SmtpClient server = new SmtpClient("xxxxxxxx",25);
server.Credentials = new NetworkCredential("xxxxx", "xxxxxxx");
SmtpClient server = new SmtpClient("mail.adalet.gov.tr",25);
server.Credentials = new NetworkCredential("xxx", "xxx");
server.Send(msg);
I suspect this might be an Encoding related issue.
Try using the new MailAddress("xxxx#xxxx.domain","blab blalb", Encoding.Default) constructor.
Else try Encoding.Unicode.
Update:
After some digging, this exception is thrown from:
void System.Net.BufferBuilder.Append(string,int,int);
This will happen if you have any characters above \xff in the email address. This is not suppose to happen, as the name should be encoded already, but something else is going funny I guess.
What headers are the message trying to send with?
You can easily dump with this MSDN snippet:
string[] keys = message.Headers.AllKeys;
Console.WriteLine("Headers");
foreach (string s in keys)
{
Console.WriteLine("{0}:", s);
Console.WriteLine(" {0}", message.Headers[s]);
I received this error when if this is modified to "network" --- then error got resolved. ( My understanding is - Incase of specified pickupdirectory option, the header -encoding utf-8 (base64) was giving error )
Hope it helps
Related
What I have done so far is to connect to EWS, access my inbox, create an item (email) with some info in Body, Subject, From, and To, save it to the Draft folder, and finally move it to my inbox. It works, however, I get a draft in the inbox instead of an email.
Is it possible to get the message as an email with the above scenario and how can I achieve that?
Below is my code. Any input would be very appreciated.
try {
message.Save();
}
catch(Exception e21) {;
}
message.Load(PS);
message.From = new EmailAddress("someone#abc.com");
message.ToRecipients.Add("me#abc.com");
message.Body = "This is A test take 1";
message.Subject = "Testing to send as someone else...";
// add in the attachments......
message.Update(ConflictResolutionMode.AlwaysOverwrite); // require this here why????
message.Copy(theTempFolder.Id); // get the item as a draft in my mailbox instead of an email
}
catch(Exception e99) {
Console.WriteLine("Exception fail to connect to office 365 on the cloud: " + e99.Message);
}
You need to set the MessageFlags property https://learn.microsoft.com/en-us/office/client-developer/outlook/mapi/pidtagmessageflags-canonical-property to 1 (before you call update) eg
ExtendedPropertyDefinition PR_MESSAGE_FLAGS_msgflag_read = new ExtendedPropertyDefinition(3591, MapiPropertyType.Integer);
message.SetExtendedProperty(PR_MESSAGE_FLAGS_msgflag_read, 1);
Which will then make the message look like it was received. The other way is just import an EML like https://learn.microsoft.com/en-us/exchange/client-developer/exchange-web-services/how-to-import-items-by-using-ews-in-exchange
Have you tried to send it to yourself with either message.Send() or message.SendAndSaveCopy() ? see more here
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 developing an windows application in which i need to send some files as attachment through email.
Code
public string SendMail(string mFrom,
string mPass,
string mTo,
string mSub,
string mMsg,
string mFile,
bool isDel)
{
string sql = "";
try
{
System.Net.Mail.MailAddress mailfrom = new System.Net.Mail.MailAddress(mFrom);
System.Net.Mail.MailAddress mailto = new System.Net.Mail.MailAddress(mTo);
System.Net.Mail.MailMessage newmsg = new System.Net.Mail.MailMessage(mailfrom, mailto);
newmsg.IsBodyHtml = false;
if (mFile.Length > 2
&& File.Exists(mFile))
{
System.Net.Mail.Attachment att = new System.Net.Mail.Attachment(mFile);
newmsg.Attachments.Add(att);
}
newmsg.Subject = mSub;
newmsg.Body = mMsg;
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("smtp.gmail.com", 587);
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential(mFrom, mPass);
smtp.EnableSsl = true;
smtp.Send(newmsg);
newmsg.Dispose();
GC.Collect();
sql = "OK";
if (isDel
&& File.Exists(mFile))
{
File.Delete(mFile);
}
}
catch (Exception ex)
{
sql = ex.Message;
}
return sql;
}
This code works fine for small files.But i need to send large files up to 1-2 GB.
For that what to do.
You cannot use e-mail to get these files across and this has nothing to do with your code.
I don't think there is ANY provider out there who will support sending files of that size let alone receiving them. Even G-Mail has a limit of 25 Mb which is quite large already.
E-Mail is not the proper channel to do this.
So the problem will not be in your code, the provider will limit the size of the attachment and just refuse them when you present them with a larger file. You will get an e-mail back at your FROM address stating that the file is too large and your e-mail did not get across.
For doing this in the simplest form probably look at FTP.
I do agree with Gerald Versluis in that email is not the proper channel for this. Even if you are using your own email server that is configurable there is probably some internal limit that prevents it from sending such big files.
I’d go with FTP for this but if you really want to continue with email I’d suggest you check following first.
Is there connection timeout property on the server? If yes then try to increase it to 3 hours or something like that.
Is there enough space on the mail server?
Is there some documentation for your email server? Are there any additional details regarding attachment size ?
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.
I have a script that sends an e-mail as both plain text and HTML, and it works fine for most e-mail readers including Outlook and Gmail. However, when reading the message on a Windows Mobile smartphone, the output is:
PCFET0NUWVBFIEhUTUwgUFVCTElDICItLy9XM0MvL0RURCBIVE1MIDMuMiBGaW5hbC8vRU4i Pg0KPEhUTUw+DQo8SEVBRD4NCiAgICA8TUVUQSBIVFRQLUVRVUlWPSJDb250ZW50LVR5cGUi IENPTlRFTlQ9InRleHQvaHRtbDtjaGFyc2V0PWlzby04ODU5LTEiPg0KICAgIDxUSVRMRT5Z b3VyIE1lZ2Fwb255IFBhc3N3b3JkIC0gTWVnYXBvbnkgLSBEaXNjb3ZlciB0aGUgbmV4dCBi aWcgdGhpbmc8L1RJVExFPg0KICAgIDxTVFlMRSBUWVBFPSJ0ZXh0L2NzcyI+DQogICAgICAg IGE6bGluaywgYTp2aXNpdGVkDQogICAgICAgIHsNCiAgICAgICAgICAgIHRleHQtZGVjb3Jh dGlvbjogdW5kZXJsaW5lOw0KICAgICAgICAgICAgY29sb3I6ICM5MDA7DQogICAgICAgIH0N CiAgICAgICAgYTpob3Zlcg0KICAgICAgICB7DQogICAgICAgICAgICB0ZXh0LWRlY29yYXRp b246IG5vbmU7DQogICAgICAgICAgICBjb2xvcjogIzAwMDsNCiAgICAgICAgfQ0KICAgIDwv U1RZTEU+DQo8L0hFQUQ+DQo8Qk9EWT4NCiAgICA8QSBIUkVGPSJodHRwOi8vd3d3Lm1lZ2Fw b255LmNvbS8iIFRJVExFPSJNZWdhcG9ueSAtIERpc2NvdmVyIHRoZSBuZXh0IGJpZyB0aGlu ZyI+DQogICAgICAgIDxJTUcgU1JDPSJodHRwOi8vd3d3Lm1lZ2Fwb255LmNvbS9faW1nL21l Z2Fwb255LWhlYWRlci1yZW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ipUaGlzIGlzIGFuIGF1dG9tYXRl ZCBtZXNzYWdlLiBQbGVhc2UgZG8gbm90IHJlcGx5LjwvRk9OVD4NCjwvQk9EWT4NCjwvSFRN TD4NCg==
The correct output should be: Click here to see
The code is:
SmtpClient mC = new SmtpClient(ConfigurationManager.AppSettings["smtpServer"]);
NetworkCredential nC = new NetworkCredential(ConfigurationManager.AppSettings["smtpUsername"], ConfigurationManager.AppSettings["smtpPassword"]);
mC.UseDefaultCredentials = false;
mC.Credentials = nC;
MailAddress mFrom = new MailAddress("noreply#megapony.com", "Megapony");
MailAddress mTo = new MailAddress(forgotpwemail.Text);
MailMessage mMsg = new MailMessage(mFrom, mTo);
mMsg.IsBodyHtml = false;
mMsg.Subject = "Your Megapony Password";
mMsg.Body = getForgotPWBodyPlain(result);
System.Net.Mime.ContentType mimeType = new System.Net.Mime.ContentType("text/html");
AlternateView alternate = AlternateView.CreateAlternateViewFromString(getForgotPWBody(result), mimeType);
mMsg.AlternateViews.Add(alternate);
try
{
mC.Send(mMsg);
pnlpwform.Visible = false;
pnlSuccess.Visible = true;
}
catch (Exception)
{
pnlResponse.Visible = true;
}
mMsg.Dispose();
Please help!
Thanks,
Paul
I would say it's a combination of the content-type and the the image that's causing the issue. The link you provided showed a nice gif with a Megapony logo, yet the content-type is set to text only. Because of this, the bits that make up the gif is being treated as a series of text characters.
This would be a good place to start: http://www.systemnetmail.com/faq/3.1.3.aspx