I have problem in sending mail by smtp and gmail in C# winform application.
without attachment sending is successfully but when attachment is added application was thrown to System.Net.Mail.SmtpException and it's message is "The operation has timed out."
what's the problem?!
SmtpClient smtpClient = new SmtpClient();
Attachment att=new System.Net.Mail.Attachment("Path");
smtpClient.EnableSsl = true;
smtpClient.Port = 25;
smtpClient.Timeout = 20000;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.UseDefaultCredentials = false;
smtpClient.Host = "smtp.gmail.com";
smtpClient.Credentials = new System.Net.NetworkCredential
(from#gmail.com, "password" );
System.Net.Mail.MailAddress from = new System.Net.Mail.MailAddress(from#gmail.com,
from,
System.Text.Encoding.UTF8);
System.Net.Mail.MailAddress to = new System.Net.Mail.MailAddress("to#gmail.com");
MailMessage message = new MailMessage(from, to);
message.Body = "some text";
message.BodyEncoding = System.Text.Encoding.UTF8;
message.Subject = "Subject";
message.SubjectEncoding = System.Text.Encoding.UTF8;
message.Bcc.Add(bcc#gmail.com);
message.Attachments.Add(att);
try
{
smtpClient.Send(message);
}
catch (System.Net.Mail.SmtpException ex)
{
MessageBox.Show(ex.Message); // The operation has timed out
}
Edit
i have this problem both of gmail and yahoo!
I don't see any major flaw in your code and I think GMail raised its attachment size limit to 50Mb some time ago... anyway, I suggest you to proceed with the following modifications:
Increase your timeout as much as possible, this alone could solve your problem if you are dealing with very big attachments: smtpClient.Timeout = Int32.MaxValue;.
Try to use different server port. Maybe you have other applications (Outlook, for example) using the port 25 to communicate and this creates a conflict. Switch to 465. If it doesn't work, try with 587.
Try to make a test using a different account.
Related
Got an issue about C# mailing.
According to rfc format has no problem when BCC added to the custom header!
However according to MSDN BCC info that is added to custom header will be deleted!
So in this particular situation I have to send an email trough C# code to a server that accepts BCC info in the custom header only...
How can I achive that? So far what I've tried is below;
// Mail Configurations
MailMessage mail = new MailMessage();
mail.DeliveryNotificationOptions = DeliveryNotificationOptions.None;
mail.Priority = MailPriority.Normal;
mail.BodyEncoding = System.Text.Encoding.UTF8;
mail.HeadersEncoding = System.Text.Encoding.UTF8;
mail.SubjectEncoding = System.Text.Encoding.UTF8;
mail.BodyTransferEncoding = System.Net.Mime.TransferEncoding.Base64;
mail.From = new MailAddress(FROM, FROM, System.Text.Encoding.UTF8); // From
mail.To.Add(new MailAddress(TO, TO, System.Text.Encoding.UTF8)); // EmailTo
// mail.CC.Add(new MailAddress(CC, CC, System.Text.Encoding.UTF8)); // CC
// mail.Bcc.Add(new MailAddress(BCC, BCC, System.Text.Encoding.UTF8)); // BCC
mail.Headers.Add("BCC", BCC); // BCC 2nd method adding in headers as PHP does
mail.Subject = Subject;
mail.IsBodyHtml = true;
mail.Body = "<div>Hello World!<div>";
mail.Attachments.Add(new Attachment(FileName));
// SMTP Configurations
SmtpClient SmtpServer = new SmtpClient(SMTP, PORT);
SmtpServer.UseDefaultCredentials = false;
SmtpServer.DeliveryFormat = SmtpDeliveryFormat.International;
SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network;
SmtpServer.EnableSsl = true;
SmtpServer.Credentials = new NetworkCredential(SMTPUser, SMTPPass);
// Send Mail
SmtpServer.Send(mail);
// Clear All
mail.To.Clear();
mail.Bcc.Clear();
mail.Headers.Clear();
mail.Attachments.Clear();
Edit: Depending on the comments I've also tried MailKit library.
It had no effect on BCC the code I've used is;
// Mail
var message = new MimeMessage();
message.From.Add(new MailboxAddress(System.Text.Encoding.UTF8, FROMName, FROM));
message.To.Add(new MailboxAddress(System.Text.Encoding.UTF8, TO, TO));
message.Cc.Add(new MailboxAddress(System.Text.Encoding.UTF8, CC, CC));
message.Bcc.Add(new MailboxAddress(System.Text.Encoding.UTF8, BCC, BCC));
message.Subject = Subject;
message.Priority = MessagePriority.Normal;
message.Importance = MessageImportance.Normal;
message.XPriority = XMessagePriority.Normal;
// With Body Builder
var builder = new BodyBuilder();
builder.HtmlBody = "<div> Hello World! </div>"; // Set the html version of the message text
builder.Attachments.Add(FileName); // We may also want to attach some files
message.Body = builder.ToMessageBody(); // Now we just need to set the message body and we're done
// SMTP connect
using (var client = new MailKit.Net.Smtp.SmtpClient())
{
client.Connect(SMTP, SMTPPort, false);
// Note: only needed if the SMTP server requires authentication
client.Authenticate(SMTPUser, SMTPPass);
client.Send(message);
client.Disconnect(true);
}
I'm not sure if I'm following along correctly, but it sounds like you need to send a message where the raw Bcc: header is sent in the headers to the SMTP server?
This is highly non-standard and very suspect since the whole point of the Bcc: header is that it gets stripped at send time because recipients in the Bcc: header field are meant to be BLIND Carbon Copy recipients (aka, hidden to everyone who receives the message).
That said... if you REALLY REALLY do need to include the Bcc: header in the message data that gets uploaded to the SMTP server, then you can do this:
var options = FormatOptions.Default.Clone ();
options.HiddenHeaders.Add (HeaderId.ContentLength);
options.HiddenHeaders.Remove (HeaderId.ResentBcc);
options.HiddenHeaders.Remove (HeaderId.Bcc);
options.NewLineFormat = NewLineFormat.Dos;
client.Send (options, message);
Note: Currently the Resent-Bcc and Bcc headers are not hidden in the default FormatOptions, I just added those .Remove() calls to illustrate how to control which headers get hidden vs not.
The SmtpClient.Send() methods that do not take a FormatOptions argument use an internal FormatOptions that add the Bcc, Resent-Bcc, and Content-Length headers to the hidden list.
This question already has answers here:
Gmail Error :The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required
(21 answers)
Closed 7 years ago.
I was create sample email sender using c# but it shows an error is:
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication
Required.
private void SendHtmlFormattedEmail(string recepientEmail, string subject, string body)
{
using (MailMessage mailMessage = new MailMessage())
{
mailMessage.From = new MailAddress("sysumurugan#gmail.com");
mailMessage.Subject = subject;
mailMessage.Body = body;
mailMessage.To.Add(new MailAddress(recepientEmail));
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = false;
System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential("sysumurugan#gmail.com", "XXXXXXXXX");
smtp.Credentials = NetworkCred;
smtp.Timeout = 10000;
smtp.Send(mailMessage);
}
}
please help me for sample mail sender code
Status:System.Net.Mail.SmtpStatusCode.MustIssueStartTlsFirst
Try swapping these lines like so:
smtp.UseDefaultCredentials = false;
System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential("sysumurugan#gmail.com", "XXXXXXXXX");
I went to the following link:
https://www.google.com/settings/security/lesssecureapps
and turned on the option Access for less secure apps
I have a problem with formatting:
MailMessage mail = new MailMessage(" ", radTextBox_To.Text,
radTextBox_Subject.Text, textControl.Text);
SmtpClient client = new SmtpClient("smtp.mail.ru");
client.Port = 25;
client.Credentials = new System.Net.NetworkCredential("login", "password");
client.EnableSsl = true;
client.Send(mail);
MessageBox.Show("Mail Sent!", "Success", MessageBoxButtons.OK);
The mail message is delivered as a regular text message (no pictures) although textControl.Text contains a picture, text, different fonts. When the message is received, it comes across as standard text.
By default, the MailMessage.Body property would render as plain text. You can change it to HTML by adding the following line of code after your declaration and prior to sending:
mail.IsBodyHtml = true;
Reference: MailMessage.IsBodyHtml Property
I am currently using this code in C# to email a file to my hotmail address with an attachment. I ran this and it worked great when attaching a 800KB file, but when I try to attach a 12MB file it just won't do it, I put it in a try catch but there were no exceptions, like it just skipped it. The email attachment size limit for hotmail is 25MB, would this affect the email code, or is there a separate limit when doing it through code? Thanks.
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.live.com");
mail.From = new MailAddress("email#hotmail.co.uk");
mail.To.Add("receiving#hotmail.co.uk");
mail.Subject = "Emailed from C#";
mail.Body = "Emailed with attachment";
Attachment attachment;
attachment = new Attachment(#"C:\file.txt");
mail.Attachments.Add(attachment);
SmtpServer.Port = 587;
SmtpServer.Credentials = new NetworkCredential("email#hotmail.co.uk", "password");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
Accepted answer is Mohamed's link to bug fix question - https://connect.microsoft.com/VisualStudio/Downloads/DownloadDetails.aspx?DownloadID=30226
I tried to send email from c# using SmtpClient.Send() but it always goes to the junk box. It works fine if I send it from Outlook. Is there anyway to solve this? Someone told me to modify the email header but I don't know how.
Thanks in advance.
Here is my code
SmtpClient client = new SmtpClient();
client.Host = "smtp.server.com";
client.Credentials = new System.Net.NetworkCredential("user", "password");
MailAddress mailFrom = new MailAddress("mymail#server.com");
MailAddress mailTo = new MailAddress("yourmail#server.com");
MailAddress mailReply = new MailAddress("mymail#server.com");
MailMessage message = new MailMessage(mailFrom, mailTo);
message.Body = "This is a test message.";
message.Subject = "test message";
message.SubjectEncoding = System.Text.Encoding.UTF8;
message.BodyEncoding = System.Text.Encoding.UTF8;
client.Send(message);
a) The code sample doesn't actually use the mailReply address.
b) The problem will probably disappear when you send a more realistic message. If it doesn't then you will have to find out why the message is being marked junk, fishing a message from the spambox and looking at the headers or something like that.
Spam filters may discard messages that have invalid entries.
Try putting in valid (existing) addresses of sender, reply and from.