How to fix unstable smtp mail service? - c#

I have an ASP.NET Core 3.1 Web API project. I am using Entity Framework.
When I pass data statically to a smtp client, mail is sent and there is no problem:
SmtpClient smtp = new SmtpClient();
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential("development#mail.com.tr", "password");
smtp.Port = 587;
smtp.Host = "mail.com.tr";
smtp.EnableSsl = false;
MailMessage mail = new MailMessage();
mail.To.Add("atakan#mail.com.tr");
mail.From = new MailAddress("development#mail.com.tr");
mail.Subject = PostaSubject;
mail.IsBodyHtml = true;
mail.Body = MailTemplate(subject, subject);
smtp.Send(mail);
But when I get the smtp client data from the data in the context, sometimes the mail is sent, sometimes it cannot be thrown and it works unstable. How can I fix it?
SmtpClient smtp = new SmtpClient();
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential(_context.MailConfigs.Select(x => x.UserName).FirstOrDefault(), _context.MailConfigs.Select(x => x.UserPassword).FirstOrDefault());
smtp.Port = (int)_context.MailConfigs.Select(x => x.SmtpPort).FirstOrDefault();
smtp.Host = _context.MailConfigs.Select(x => x.SmtpHost).FirstOrDefault();
smtp.EnableSsl = false;
MailMessage mail = new MailMessage();
mail.To.Add(_context.MailConfigs.Select(x => x.Reciever).FirstOrDefault());
mail.From = new MailAddress(_context.MailConfigs.Select(x => x.UserName).FirstOrDefault());
mail.Subject = PostaSubject;
mail.IsBodyHtml = true;
mail.Body = MailTemplate(subject, subject);
smtp.Send(mail);

FirstOrDefault will return a value or null, why not use a string variable to capture the value of FirstOrDefault result and if it is not null then send the email or if the value is null then just ignore or write to a log.

Related

Email configuration

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

exception while sending mail multiple recipient

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

Sending a gmail email

I am trying to send an email through and C# application (Console). Actually I would like it to send an email to any type of email but for the time being if I can get it to send to a gmail account I would be happy enough.
I just want to be able to get this to send to a gmail account for the time being?
Whole Program:
namespace EmailAddress
{
class Program
{
static void Main(string[] args)
{
Program test = new Program();
test.email_send();
}
public void email_send()
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("hg#gmail.com");
mail.To.Add("hg#gmail.com");
mail.Subject = "Test Mail - 1";
mail.Body = "Your attachment is accessible on your computer!";
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment("g:/example1.txt");
mail.Attachments.Add(attachment);
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("your mail#gmail.com", "your password");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
}
}
}
New code: Doesn't hang but will not send the message to the inbox
static void Main(string[] args)
{
Program test = new Program();
//test.email_send();
test.CreateTestMessage4();
}
public void email_send()
{
MailMessage mail = new MailMessage();
SmtpClient smtp = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("g#gmail.com");
mail.To.Add("g#gmail.com");
mail.Subject = "Test Mail - 1";
mail.Body = "Your attachment is accessible on your computer!";
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment("g:\\example1.txt");
mail.Attachments.Add(attachment);//list of attachements
smtp.Port = 587;//google standard -- most of the time wouldn't not set
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Credentials = new System.Net.NetworkCredential("*","*");
smtp.Send(mail);
Console.WriteLine("-- Sending Email --");
Console.ReadLine();
}
Can someone try this code and see if it works. For some reason this isn't working so I would like to have someone's fresh perspective of this. I just call this in the main method for an instance of the class.
public void email_send()
{
MailMessage mail = new MailMessage();
SmtpClient smtp = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("your email address");
mail.To.Add("your email address");
mail.Subject = "Test Mail - 1";
mail.Body = "Your attachment is accessible on your computer!";
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment("g:\\example1.txt");
mail.Attachments.Add(attachment);//list of attachements
//smtp.Port = 587;//google standard -- most of the time wouldn't not set
//smtp.EnableSsl = true;
//smtp.UseDefaultCredentials = true;
//smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
//smtp.Credentials = new System.Net.NetworkCredential("your address",
"your password");
smtp.Port = 587;
smtp.Host = "smtp.gmail.com";
smtp.UseDefaultCredentials = false;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.EnableSsl = true;
smtp.Credentials = new System.Net.NetworkCredential("*", "*"); smtp.Send(mail);
Console.WriteLine("-- Sending Email --");
Console.ReadLine();
}
Your code is close:
MailMessage mail = new MailMessage();
using (SmtpClient smtp = new SmtpClient("smtp.gmail.com")) {
mail.From = new MailAddress("haufdoug#gmail.com");
mail.To.Add("haufdoug#gmail.com");
mail.Subject = "Test Mail - 1";
mail.Body = "Your attachment is accessible on your computer!";
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment("g:\\example1.txt");
mail.Attachments.Add(attachment);
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = false;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Credentials = new System.Net.NetworkCredential("your mail#gmail.com", "your password");
smtp.Send(mail);
}
Now, regarding some of your other questions.
Port 587 is used by Google only. Typically the mail server you connect to will tell you what port to use for SMTP mail; Google said there's is 587.
For sending through other servers you typically won't set the port number.
side notes:
SmtpClient implements the IDisposable interface. So it should be wrapped in a using clause to ensure it is properly disposed of when complete. Unless you are using the .SendAsync() method.
I changed the variable name from SmtpServer to smtp for two reasons. First, naming convention. Variables inside a method are recommended to be camel case (forExampleThis). Second, SmtpServer is a misnomer as the object is an SmtpClient. Those are very different things and it's just bad practice to misname things.
Can you try this, it's working for me:
SmtpClient SmtpServer = new SmtpClient();
SmtpServer.Port = 587;
SmtpServer.Host = smtp.gmail.com;
SmtpServer.UseDefaultCredentials = false;
SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network;
SmtpServer.EnableSsl = true;
SmtpServer.Credentials = new System.Net.NetworkCredential("your mail#gmail.com", "your password");

System.Security.Cryptography.CryptographicException: The handle is invalid

I am getting exception while sending mail through C# for
SmtpClient client = new SmtpClient()
as System.Security.Cryptography.CryptographicException: The handle is invalid.
MailMessage mail = new MailMessage(from,to);
mail.To.Add(to);
mail.From = new MailAddress(from, "", System.Text.Encoding.UTF8);
mail.Subject = "This is a test mail";
mail.SubjectEncoding = System.Text.Encoding.UTF8;
mail.Body = fr.message;
mail.BodyEncoding = System.Text.Encoding.UTF8;
mail.IsBodyHtml = true;
mail.Priority = MailPriority.High;
SmtpClient client = new SmtpClient();
//Add the Creddentials- use your own email id and password
client.Credentials = new System.Net.NetworkCredential(from, Password);
client.Port = 587; // Gmail works on this port
client.Host = "smtp.gmail.com";
client.EnableSsl = true; //Gmail works on Server Secured Layer
client.Send(mail);
I am not getting why this happens?
Check your project settings and make sure you have checked NTLM Authentication:

.Net send mail through Gmail [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Sending email in .NET through Gmail
I am trying to send a mail with Asp.Net (MVC3) through an GMail-account i've created. I've crawled the internet for how-to's, but nothing i tried has been working.
I have a method that looks like this
public static bool SendEmail(string to,string subject,string body)
{
try
{
MailMessage mail = new MailMessage();
mail.From = new MailAddress("myaccount#gmail.com");
mail.To.Add(to);
mail.Subject = subject;
mail.Body = body;
mail.IsBodyHtml = false;
SmtpClient smtp = new SmtpClient();
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.Credentials = new System.Net.NetworkCredential("myaccount#gmail.com", "mypassword");
smtp.EnableSsl = true;
smtp.Send(mail);
return true;
}
catch
{
return false;
}
}
This method returns false when im using it. How can i solve this?
I verified the following works:
static void Main(string[] args)
{
//create the mail message
MailMessage mail = new MailMessage();
var client = new SmtpClient("smtp.gmail.com", 587)
{
Credentials = new NetworkCredential("your.email#gmail.com", "yourpassword"),
EnableSsl = true
};
//set the addresses
mail.From = new MailAddress("your.email#gmail.com");
mail.To.Add("to#wherever.com");
//set the content
mail.Subject = "Test subject!";
mail.Body = "<html><body>your content</body></html>";
mail.IsBodyHtml = true;
client.Send(mail);
}
Try Adding this line
smtp.UseDefaultCredentials = false;

Categories

Resources