Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I want to send e-mail in C# and use the mail addresses in the MySQL database. I am totally a beginner by the way. This is what i did so far :
I created a method for sending e mail:
public static void sendEmail()
{
DataClassesDataContext dc = DataContext;
var _userMail = (from u in dc.Users
select u.Email).ToString();
foreach (var item in _userMail)
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("mymail#gmail.com");
mail.To.Add(_userMail);
mail.Subject = "Test Mail";
mail.Body = "This is for testing SMTP mail from GMAIL";
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("myusername", "mypassword");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
}
And i am calling this method in one of my forms :
try
{
AppMethod.sendEmail();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
I wanted to take all mails from User table with using the _userMail and that's why I convert it to string. What should i do ? I mean where my mistake is, i couldnt find.
I assume that Item contain your email Id then you can use below menioned code
and if you want collection of mails then your query should be
var _userMail = dc.Users.Select(p=>p.Email);
just replace
mail.To.Add(_userMail);
to
mail.To.Add(item);
if Item is Object of User then you have to tried below mentioned code
mail.To.Add(item.emailId);
Related
Alright so pretty much I know there is a simple solution for this for the life of me though I can't find it. I want to send an attachment via mail, now I have it so that it thinks it's going to send an attachment like:
message.To.Add(recieve + "#txt.att.net");
message.From = new MailAddress(user);
message.Subject = subject;
message.Body = body;
message.Attachments.Add(new Attachment(add_photo.FileName));
client.Send(message);
You know but if add_photo(The File Dialog) is emtpy it throws and error, I tried adding a catch statement for it but the program just kinda crashes almost (not like crashes crashes but functionality wise).
Anyway, I was thinking if there is no file selected by the dialog I'll just set one myself, something really small that wouldn't even matter. So I have a picture in my resources called 'DD.png' and I would like to set it if there is no file in the dialog any ideas?
Here's what I have:
if (!string.IsNullOrEmpty(add_photo.FileName))
{
add_photo.FileName = (Path.GetFullPath(Turbo_Bomber.Properties.Resources.DD.ToString()));
}
#region Providers
if (provider == "AT&T")
{
message.To.Add(recieve + "#txt.att.net");
message.From = new MailAddress(user);
message.Subject = subject;
message.Body = body;
message.Attachments.Add(new Attachment(add_photo.FileName));
client.Send(message);
} // etc
Any ideas? Thank you guys.
Stick with your first go, with a small change:
message.To.Add(recieve + "#txt.att.net");
message.From = new MailAddress(user);
message.Subject = subject;
message.Body = body;
if (!string.IsNullOrEmpty(add_photo.FileName))
{
message.Attachments.Add(new Attachment(add_photo.FileName));
}
client.Send(message);
Now you don't need to add a 'mystery' attachment.
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 want to create a mailing list via asp.net .I've studied a lots of article about it . but all of them were the same .in those article was written that I should use this code
var list = from c in context.Emails orderby c.EmailAddress select c.EmailAddress;
MailMessage mail = new MailMessage();
foreach (var c in list)
{
try
{
mail.From = new MailAddress(txtfrom.Text);
mail.To.Add(new MailAddress(c.ToString()));
mail.Subject = txtSub.Text;
mail.IsBodyHtml = true;
mail.Body = txtBody.Text;
if (FileUpload1.HasFile)
{
mail.Attachments.Add(new Attachment(FileUpload1.PostedFile.InputStream, FileUpload1.FileName));
}
SmtpClient smtp = new SmtpClient();
smtp.Send(mail);
}
catch (Exception)
{
}
}
so the question is that ,is this way really useful and successful to sending lots of emails? (for example 2000 emails?)
in those articles was written that i should put delay after each period times (for example after sending 50 emails).and I wanna know how to make delay between sending emails.
I'm looking for a perfessional way to create this project
I was wondering if someone gives me open source mailing list in asp.net
I'd change the code like this
var list = from c in context.Emails orderby c.EmailAddress select c.EmailAddress;
MailMessage mail = new MailMessage();
try
{
mail.From = new MailAddress(txtfrom.Text);
foreach (var c in list)
{
mail.To.Add(new MailAddress(c.ToString()));
}
mail.Subject = txtSub.Text;
mail.IsBodyHtml = true;
mail.Body = txtBody.Text;
if (FileUpload1.HasFile)
{
mail.Attachments.Add(new Attachment(FileUpload1.PostedFile.InputStream, FileUpload1.FileName));
}
SmtpClient smtp = new SmtpClient();
smtp.Send(mail);
}
catch (Exception)
{
//exception handling
}
At least, smtp.Send() is invoked only once.
Try using multiple threads, here is an example; msdn forum sendin bulk mails
Probably, you have some time limit for running you script (i.e 30 seconds). I suggest split into 2 steps:
format e-mails you need and write them into database table (with status - "not sent")
run another script/program, select N e-mails from table, send and mark them as "sent"
wait/sleep N seconds
repeat 2nd step
This lets you to send almost unlimit number of e-mails, w/o timeout
Also - pay attention if you have some limit on sending e-mails per hour/day on your hosting!
Create work items for emails to be sent and push them to queue. Then use as many competing consumers (aka workers) as you want to send those emails.
The asp app only needs to create an XML with the info and save it.
Use a windows service with a filewatcher. This service only detect the creation of the list in XML and send it.
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.
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