C# Send E-Mail issue - c#

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

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

How to Send Email using c# on Go Daddy Server

I'm attaching the code below:
I get an Error in this Code But This Code Perfectly Working at My Local
IIS.But not Working at Any Server,
My Code was
Email-Code
MailMessage msg = new MailMessage();
msg.From = new MailAddress("From Mail ID");
msg.To.Add("To Mail ID");
msg.Subject = "Hello world! " + DateTime.Now.ToString();
msg.Body = "hi to you ... :)";
SmtpClient client = new SmtpClient();
client.Host = "smtp.gmail.com";
client.Port = 587;
client.EnableSsl = true;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("From Mail ID", "******");
client.Timeout = 20000;client.Send(msg);
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient();
mail.To.Add("*****#gmail.com");
mail.From = new MailAddress("domain email");
mail.Subject = "Test Email";
mail.IsBodyHtml = true;
mail.Body = "Test";
SmtpServer.Host = "dedrelay.secureserver.net";
SmtpServer.Port = 25;
SmtpServer.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
try {
SmtpServer.Send(mail);
} catch (Exception ex) {
throw ex;
}

Not able to send E-mail through IIS server (Working in local )

I've been working on this from months, please someone help me solve this. Thanks
try
{
SmtpClient smtpServer = new SmtpClient();
MailMessage mail = new MailMessage();
smtpServer.Credentials = new System.Net.NetworkCredential("iejabdb#otlook.com", "throughput");
smtpServer.Port = 25;
smtpServer.Host = "*****";
smtpServer.EnableSsl = true;
smtpServer.UseDefaultCredentials = true;
mail.From = new MailAddress("*****#outlook.com", "Work Order System");
mail.To.Add("*******#outlook.com");
// mail.To.Add(receiptsArray.ToString());
mail.Subject = "Test";
mail.Body = "Hello";
smtpServer.Send(mail);
}
Test this Code:
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.live.com");
mail.From = new MailAddress("mawlud.f.m#hotmail.com");
mail.To.Add("mawlud.f.m#gmail.com");
mail.Subject = "Mail Subject";
mail.Body = "Mail Body";
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("mawlud.f.m#hotmail.com", "Write Your Password");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);

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

Categories