A C# gmail MailSender - c#

I have created an email sending function but it keeps getting timed out.
try
{
send.Enabled = false;
MailMessage message = new MailMessage();
message.From = new MailAddress(senderemail.Text);
message.Subject = subject.Text;
message.Body = body.Text;
foreach (string str in recipients.Text.Split(';'))
{
message.To.Add(str);
}
try
{
SmtpClient client = new SmtpClient();
client.Credentials = new NetworkCredential(senderemail.Text, senderpassword.Text);
client.Host = "smtp.gmail.com";
client.Port = 587;
client.EnableSsl = true;
client.Send(message);
MessageBox.Show("Sent Successfully", "Details");
}
catch
{
SmtpClient client2 = new SmtpClient();
client2.Credentials = new NetworkCredential(senderemail.Text, senderpassword.Text);
client2.Host = "smtp.gmail.com";
client2.Port = 465;
client2.EnableSsl = true;
client2.Send(message);
MessageBox.Show("Sent Successfully", "Details");
}
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString(), "Error");
}
finally
{
send.Enabled = true;
}
After a port analysis using telnet, I am clueless as to why the code won't execute properly.

Try
client.UseDefaultCredentials = false;

Related

C# smtp doesn't want to send an email

when sending an email, I get an exception:
Sending mail failed.
I've set the host to "smtp.gmail.com" and port to 587 but it doesn't work.
Code:
public void SendActivationLink(string email)
{
using (MailMessage mail = new MailMessage())
{
SmtpClient smtpClient = new SmtpClient();
smtpClient.UseDefaultCredentials = false;
smtpClient.EnableSsl = true;
smtpClient.Port = 587;
MailMessage message = new MailMessage();
MailAddress from = new MailAddress("myemail#gmail.com", "Voc");
message.From = from;
message.To.Add(email);
message.Subject = "Title";
message.Body = "Jello";
smtpClient.Host = "smtp.gmail.com";
smtpClient.UseDefaultCredentials = false;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.Credentials = new System.Net.NetworkCredential("myemail#gmail.com", "mypassword");
try
{
smtpClient.SendAsync(message, email);
}
catch (SmtpException ex)
{
throw new ApplicationException("exeption" + ex.Message);
}
}

System.Net.Mail and MailMessage not Sending Messages

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"),

null value when sending email via MVC ASP.NET

I have this MVC C# App and a controller where trying to send a email, but I always got this error
this is the code in my action´controller, it doesn´t receive any model so I add specific values
[HttpPost]
public ActionResult Index2()
{
try
{
MailMessage mail = new MailMessage();
mail.To.Add("valid_email#hotmail.com");
mail.From = new MailAddress("valid_email#gmail.com");
mail.Subject = "PruebaMVC";
string Body = "PruebaMVC";
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
("valid_email#gmail.com", "correctPWD");
smtp.EnableSsl = true;
smtp.Send(mail);
}
catch (Exception ex)
{
throw new ArgumentNullException("Exception in sendEmail:" + ex.Message);
}
return View();
}
could you please tell what is wrong?
edit: this is the exception I got
As i have used below code and it's working fine for me and i have configured smtp in web.config
ContentType HTMLType = new ContentType("text/html");
NetworkCredential cred = new NetworkCredential(StringConstant.NetworkUserName, StringConstant.NetworkPwd);
MailMessage msg = new MailMessage();
msg.BodyEncoding = System.Text.Encoding.Default;
msg.To.Add(ToEmail);
msg.Priority = System.Net.Mail.MailPriority.High;
msg.Subject = Subj;
msg.Body = strBody;
msg.IsBodyHtml = true;
if (attachement != null)
{
msg.Attachments.Add(attachement);
}
System.Net.Mail.AlternateView HTMLView = System.Net.Mail.AlternateView.CreateAlternateViewFromString(strBody, HTMLType);
msg.From = new MailAddress(StringConstant.MailFrom); // Your Email Id
SmtpClient client = new SmtpClient(StringConstant.SmtpFrom, StringConstant.SmtpPort);
client.Credentials = cred;
client.EnableSsl = true;
client.Send(msg);

Can't send mails

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);
}
}

C# Send E-Mail issue

I'm trying to send an e-mail in C#,and I'm having some issues.Whenever I try to send an e-mail,I get a message "Error: Failure to send mail".
Here is my code:
try
{
client.Host = "smtp.gmail.com";
client.Port = 465;
client.UseDefaultCredentials = false;
client.Credentials = smtpCreds;
client.EnableSsl = true;
MailAddress sendFrom = new MailAddress("from#domain.com");
MailAddress sendTo = new MailAddress("to#domain.com");
MailMessage msg = new MailMessage(sendFrom,sendTo);
msg.Subject = "Subject";
msg.Body = "Body";
client.Send(msg);
}
catch (Exception e)
{
MessageBox.Show("Error:" + e.Message);
}
Also I have this declaration:
public SmtpClient client = new SmtpClient();
public System.Net.NetworkCredential smtpCreds = new System.Net.NetworkCredential("mail", "password");
Hope you can help me.
you can try this and make sure you are using valid login credential and you have internet connection:
MailMessage mail = new MailMessage();
mail.Subject = "Your Subject";
mail.From = new MailAddress("senderMailAddress");
mail.To.Add("ReceiverMailAddress");
mail.Body = "Hello! your mail content goes here...";
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.EnableSsl = true;
NetworkCredential netCre = new NetworkCredential("SenderMailAddress","SenderPassword" );
smtp.Credentials = netCre;
try
{
smtp.Send(mail);
}
catch (Exception ex)
{
}
Try this code
using System.Net.Mail;
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("sender#gmail.com");
mail.To.Add("reciever#gmail.com");
mail.Subject = ("e mail subject");
mail.Body = ("message body");
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("sender's username", "sender's password");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
MessageBox.Show("mail Send");

Categories