I have been trying to create a small program to send email through smtp.gmail.com, but it always prompt me that "The operation has timed out".
I know there are lots of solutions available on the net but none of it works.
try
{
MailMessage message = new MailMessage();
SmtpClient smtp = new SmtpClient();
message.From = new MailAddress("from#gmail.com");
message.To.Add(new MailAddress("to#gmail.com"));
message.Subject = "Test";
message.Body = "Content";
smtp.Port = 465;
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential("from#gmail.com", "pwd");
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Send(message);
}
catch (Exception ex)
{
MessageBox.Show("err: " + ex.Message);
}
Is there any way to solve this?
Change the port to 587:
try
{
MailMessage message = new MailMessage();
SmtpClient smtp = new SmtpClient();
message.From = new MailAddress("from#gmail.com");
message.To.Add(new MailAddress("to#gmail.com"));
message.Subject = "Test";
message.Body = "Content";
smtp.Port = 587;
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential("from#gmail.com", "pwd");
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Send(message);
}
catch (Exception ex)
{
MessageBox.Show("err: " + ex.Message);
}
how to how to send email of pdf file which is store in d drive in c# windows
application...the answer is...
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress(txtFrom.Text.ToString());
mail.To.Add(txtmailTo.Text.ToString());
mail.Subject = "Mail Pdf";
var filename = #"D:/your file path/.pdf";
mail.Attachments.Add(new Attachment(filename));
SmtpServer.Port = 587;
SmtpServer.Credentials = new
System.Net.NetworkCredential(txtFrom.Text, txtPassword.Text);
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
you can use SMTP protocol to send this image as an Attachment, your code will be something like that :
using MailKit.Net.Smtp; using MimeKit;
MimeMessage message = new MimeMessage();
BodyBuilder Attachmint = new BodyBuilder();
message.From.Add(new MailboxAddress("name sender", "Mail From"));
message.To.Add(MailboxAddress.Parse("Mail To"));
message.Subject = Subject;
message.Body = new TextPart("plain")
{
Text = tex_body.Text + Massage
};
Attachmint.Attachments.Add("Attatchment Path");
message.Body = Attachmint.ToMessageBody();
SmtpClient client = new SmtpClient();
client.Connect("smtp.gmail.com", 465, true);
client.Authenticate("Mail from", "Password mail");
client.Send(message);
Related
Hi guys I am doing email function for the first time and I searched up on how to do a email
try
{
MailMessage message = new MailMessage();
SmtpClient smtp = new SmtpClient();
message.From = new MailAddress("from#gmail.com");
message.To.Add(new MailAddress("to#gmail.com"));
message.Subject = "Test";
message.Body = "Content";
smtp.Port = 587;
smtp.Host = "gmail.com";
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = true;
smtp.Credentials = new NetworkCredential("from#gmail.com", "password");
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Send(message);
}
catch (Exception ex)
{
MessageBox.Show("err: " + ex.Message);
}
I tried running this but I am not sure what is the problem... i did not use IIS or anything
(update)
I uploaded the error image in this website
http://imgur.com/3vVZhsW
This one will Help you..! try this..!
try
{
MailMessage Msg = new MailMessage();
// Sender e-mail address.
SmtpClient smtp = new SmtpClient();
//smtp object.
Msg.From = new MailAddress("from#gmail.com");
// Recipient e-mail address.
Msg.To.Add(to#gmail.com);
//here you can pass textbox value how need to send mail.
Msg.Subject = "Please confirm your subscription";
Msg.Body = "<body></body>";
Msg.IsBodyHtml = true;
// your remote SMTP server IP.
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.Credentials = new System.Net.NetworkCredential("from#gmail.com", "password");
smtp.EnableSsl = true;
smtp.Send(Msg);
//Msg = null;
}
catch(Exception ex)
{
Message.Show("Error:" + ex.Message);
}
void sendMail(string invoiceNumber)
{
try
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("Smtp.gmail.com");
mail.From = new MailAddress("*******#gmail.com");
mail.To.Add("******#gmail.com");
mail.Subject = "Test Mail - 1";
mail.Body = invoiceNumber;
mail.Subject = "PDFs Attached";
DirectoryInfo di = new DirectoryInfo(#"C:\Users\User\Desktop\test\");
foreach (var file in di.GetFileSystemInfos("*.pdf*"))
mail.Attachments.Add(new Attachment(file.FullName));
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("*******#gmail.com", "*********");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
https://www.google.com/settings/security/lesssecureapps use this link to Allow less secure apps on of your gmail id....
void sendMail(string invoiceNumber)
{
try
{
MailMessage mail = new MailMessage();
mail.To.Add("recevermailid");
mail.From = new MailAddress("sender id");
mail.Subject = "Confirmation";
string Body = "your message body";
mail.Body = Body;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential
("sender email", "seneder email password");// Enter seders User name and password
smtp.EnableSsl = true;
smtp.Send(mail);
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
I am getting Time-out Error.
I am not able to send email using this code. I tried almost every
possible way but of no use.
try
{
string myString = "Hello I am new email message. I am delivered for testing.";
MailMessage mm = new MailMessage("XXXXXXXXXX", "XXXXXXXXXX");
//mm.CC.Add("XXXXXXXXXX");
mm.Subject = "Subject have been successfully placed.";
mm.Body = myString.ToString();
mm.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
NetworkCred.UserName = "XXXXXXXXXX";
NetworkCred.Password = "XXXXXXXXXX";
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 465;
smtp.Send(mm);
Request.Cookies.Clear();
}
catch (Exception ee)
{
Response.Write(ee.Message);
}
try this
private void SendMail()
{
MailMessage mail = new MailMessage();
SmtpClient mailClient = new SmtpClient("smtp.gmail.com", 587);
mail.From = new MailAddress("Setfromaddress");
mail.To.Add(new MailAddress("recepient#gmail.com"));
mail.Subject = "Test";
mail.Body = "This is a test";
mailClient.EnableSsl = true;
mailClient.Credentials = new NetworkCredential("Username", "Password");
try
{
mailClient.Send(mail);
}
catch(Exception ex)
{
WriteErrorOutput(ex.Message);
}
}
also I think we need to check the connection
To run the telnet and test on a Windows in your computer:
1.Open the Start menu, and select Run.
2.Enter command in the Open: field, and click OK.
3.Enter 'telnet smtp.gmail.com 465,' and hit Enter, or enter 'telnet smtp.gmail.com 587' instead.
I am trying to send an email to Multiple email addresses. But I am getting this exception
A recipient must be specified
at this line
smtp.Send(mail);
I have checked related posts on stackoverflow then
I have tried to add this line
mail.To.Add(new MailAddress(c[0].Address));
but this time exception message says
: Mailbox unavailable. The server response was: 5.7.3 Requested action aborted; user not authenticated
This is my code:
MailMessage mail = new MailMessage();
mail.From = new MailAddress(emailFrom);
MailAddressCollection c = new MailAddressCollection();
c.Add(new MailAddress("sendto#gmail.com"));
mail.Subject = "some word";
mail.Body = "some word";
mail.IsBodyHtml = true;
if (c[0].Address.Contains("yahoo"))
{
using (SmtpClient smtp = new SmtpClient())
{
smtp.Credentials = new NetworkCredential(emailFrom,PASS);
smtp.EnableSsl = true;
smtp.Send(mail);
}
}
else if (c[0].Address.Contains("hotmail"))
{
using (SmtpClient smtp = new SmtpClient())
{
smtp.Port = 587;
smtp.Host = "smtp.live.com";
smtp.Credentials = new NetworkCredential(emailFrom, PASS);
smtp.EnableSsl = true;
smtp.Send(mail);
}
}
else if (c[0].Address.Contains("gmail"))
{
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.Credentials = new NetworkCredential(emailFrom,PASS)
smtp.EnableSsl = true;
smtp.Send(mail);
}
SmtpClient smtpClient = new SmtpClient();
MailMessage message = new MailMessage();
try
{
MailAddress fromAddress = new MailAddress("myname#gmail.com", "Lenin");
smtpClient.Host = "localhost";
//smtpClient.Host = "";
//smtpClient.Port = 25;
message.From = fromAddress;
message.To.Add("myname#gmail.com");
message.Subject = "Feedback";
//message.CC.Add("admin1# gmail.com");
//message.CC.Add("admin2# gmail.com");
// message.Bcc.Add(new MailAddress("admin3# gmail.com"));
// message.Bcc.Add(new MailAddress("admin4# gmail.com"));
message.IsBodyHtml = false;
message.Body = txtComments.Text;
smtpClient.Send(message);
MessageBox.Show("Email successfully sent.");
}
catch (Exception ex)
{
MessageBox.Show("Send Email Failed." + ex.Message);
}
please help to sent email to different server .. gmail.com/yahoo.com/inbox.com ..etc using windows application.
thanks for the help.
SmtpClient smtpClient = new SmtpClient(host, port);
SmtpClient client = new SmtpClient();
client.Credentials = new System.Net.NetworkCredential("username#gmail.com", "password");
client.EnableSsl = true;
client.Host = "smtp.gmail.com";
client.Port = 465 or 587;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Send(MailMessage);
try this one.