SmtpClient client = new SmtpClient();
client.Port = 587;
client.Host = "smtp.gmail.com";
client.EnableSsl = true;
client.Timeout = 10000;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("*********#gmail.com", "***********");
MailMessage mm = new MailMessage("*********#gmail.com", "******#gmail.com", "delivery.", "tttt");
mm.BodyEncoding = UTF8Encoding.UTF8;
mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
Attachment fMailAttachment;
fMailAttachment = new Attachment(FilePath);
mm.Attachments.Add(fMailAttachment);
client.Send(mm);
mm.Attachments.Dispose();
I am trying to send an email using this code. It works well but only when I am logging in my mail and let it opened, when I close mail it does not send any mails.
what is the problem with that code ?
It works well after I edited
client.Timeout = 10000;
To
client.Timeout = 100000;
Related
I have update from an obsolete name space to System.Net.Mail - the code was supposed to be straight forward - I am having problems sending email and can't get hold of the issue
public bool send()
{
SmtpClient mailClient = new SmtpClient("my domain", 25);
MailMessage mailMessage = new MailMessage();
mailClient.EnableSsl = false;
mailMessage.From = new MailAddress("my email");
mailMessage.To.Add(sendTo.ToString());
//mailMessage.Bcc.Add(bcc.ToString());
//mailMessage.CC.Add(cc.ToString());
mailMessage.Subject = subject;
mailMessage.Body = body.ToString();
mailMessage.IsBodyHtml = true;
try
{
mailClient.Send(mailMessage);
}
catch (Exception exp)
{
exp.ToString();
return false;
}
return true;
}
using Gmail smtp server
var client = new SmtpClient();
client.Port = 587;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
if (!client.UseDefaultCredentials)
client.Credentials = new System.Net.NetworkCredential("your_email#mail.com", "your_email_pass");
client.EnableSsl = true;
client.Host = "smtp.gmail.com";
var mail = new MailMessage("from#mail.com", "to#mail.com");
mail.Subject = "test ";
mail.Body = "body message";
mail.IsBodyHtml = true;
client.Send(mail);
No Credential for the smtp server? Or you add it already.
Try
mailClient.Credentials = new NetworkCredential("username", "password"),
I've tried sending mails using this code:
SmtpClient client = new SmtpClient();
client.Port = 587;
client.Host = "smtp.gmail.com";
client.EnableSsl = true;
client.Timeout = 10000;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("email#gmail.com", "password");
MailMessage mail = new MailMessage("email#gmail.com", "some1#gmail.com", "Subject", "Body");
mail.BodyEncoding = UTF8Encoding.UTF8;
mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
client.Send(mail);
It doesn't work, the error looks like this: http://scr.hu/5gpw/tv6hn
What's wrong with this code?
I would change the code into a Method and test passing in the the Port, Host etc look at this and see if it works for you I just tested it and it works great on my end.
public void Send(string from, string to,string smtpServer, int smtpPort,string username, string password)
{
try
{
using (MailMessage mm = new MailMessage())
{
SmtpClient sc = new SmtpClient();
mm.From = new MailAddress(from, "Test");
mm.To.Add(new MailAddress(to));
mm.IsBodyHtml = true;
mm.Subject = "Test Message";
mm.Body = "This is a test email message from Krzysztof Senska";
mm.BodyEncoding = System.Text.Encoding.UTF8;
mm.SubjectEncoding = System.Text.Encoding.UTF8;
NetworkCredential su = new NetworkCredential(username, password);
sc.Host = smtpServer;
sc.Port = smtpPort;
sc.Credentials = su;
sc.Send(mm);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
I am trying to send a mail the form posts, but when the form posts it tries to send the mail and then times out. the code that is timing out is this:
MailMessage mail = new MailMessage();
mail.From = new MailAddress("someone#example.com");
mail.To.Add("someoneElse#example.com");
SmtpClient smtp = new SmtpClient();
smtp.Port = 465;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.UseDefaultCredentials = false;
smtp.Host = "smtp.gmail.com";
mail.Subject = "Hello";
mail.Body = "World!";
smtp.Send(mail);
This is almost certainly an issue with your client setup.
If you're using port 465, you're required to use SSL -
See here https://support.google.com/a/answer/176600?hl=en
In your current client setup this is omitted.
Also, I think you're missing your credentials.
Try changing your client to
SmtpClient client = new SmtpClient();
client.Host = "smtp.gmail.com";
client.Port = 587;
client.EnableSsl = true; //ssl is required
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("your-email#gmail.com", "YourPassword");
client.Timeout = 20000; //increase the timeout
As mentioned in the comments, you may also need to allow "less secure apps" access to your account -
Info here http://support.google.com/accounts/answer/6010255
MailAddress mailFrom = new MailAddress("test#smtp.com");
MailAddress mailTo = new MailAddress("tester#gmail.com");
MailMessage mail2 = new MailMessage(mailFrom, mailTo);
mail2.IsBodyHtml = true;
SmtpClient client = new SmtpClient();
client.Port = 25;
client.Host = "xxx.xx.xx.xxx"; // smtp host ip
mail2.Subject = "Testing.";
mail2.Body = "Hello";
mail2.SubjectEncoding = System.Text.Encoding.UTF8;
mail2.BodyEncoding = System.Text.Encoding.UTF8;
client.Send(mail2);
the above is my function that use to send an email via smtp, but I realized all the mail was located in my spam mail folder (Gmail). Is there anyway that can solve it ?
IsBodyHTML is marked true, but you're only providing text/html. You minimally need to include an alternate view with text
mail2.Body = "Hello";
make sure you not using Mail from and mailto is same address or
MailMessage mail2 = new MailMessage(mailFrom, mailTo);
UPDATE
mail2.IsBodyHtml = true;
SmtpClient client = new SmtpClient();
client.Port = 25;
client.Host = "xxx.xx.xx.xxx"; // smtp host ip
mail2.Subject = "Testing.";
mail2.Body = "Hello";
string html = "html";
// here is example to user AlternateViews
mail2.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(html, new System.Net.Mime.ContentType("text/html"));
string Plaintext ="plain text";
mail2.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(Plaintext, new System.Net.Mime.ContentType("text/plain"));
mail2.SubjectEncoding = System.Text.Encoding.UTF8;
mail2.BodyEncoding = System.Text.Encoding.UTF8;
client.Send(mail2);
Try this and revert.
string mailServer;
int port;
string mailId, mailPass;
string subject;
string mailTo;
subject="something";
StringBuilder mailBody = new StringBuilder();
mailTo = "someone#gmail.com";
mailServer = "smtp.gmail.com";
mailId = "something#gmail.com";
myString.Length = 0;
myString.Append("<html><body><div>BODY CONTENT</div></body></html>");
mailPass = "xxxxxx";
port = 587;
MailMessage mail = new MailMessage(mailId, mailTo, subject, myString.ToString());
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient(mailServer, port);
System.Net.NetworkCredential nc = new System.Net.NetworkCredential(mailId, mailPass);
smtp.UseDefaultCredentials = false;
smtp.Credentials = nc;
smtp.EnableSsl = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Send(mail);
I have the following code which is not working:
public static void SendMail(string content, string title, List<string> address)
{
SmtpClient client = new SmtpClient(Server,Port);
client.Port = Port;
client.Host = Server;
client.EnableSsl = false;
client.Timeout = 10000;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(Username, Password);
foreach(string to in address)
{
MailMessage mm = new MailMessage(From, to, title, content);
mm.BodyEncoding = UTF8Encoding.UTF8;
mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
client.Send(mm);
}
client.Dispose();
}
I am getting the following error:
Mailbox unavailable. The server response was: You must give your username and password to send mail through this service
You can see that I am passing a username and password. Why am I still getting this issue?
here i am using example of using gmail server
MailMessage mail = new MailMessage();
mail.To.Add(textBox1.Text);
mail.From = new MailAddress("Yourgmailid");
mail.Subject = "Email using Gmail";
string Body = "Hi, this mail is to test sending mail" +
"using Gmail in ASP.NET";
mail.Body = Body;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Credentials = new System.Net.NetworkCredential
("Yourgmailid, "Password");
smtp.EnableSsl = true;
smtp.Send(mail);