exception while sending mail multiple recipient - c#

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

Related

How to fix unstable smtp mail service?

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.

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

Not able to send emails through my asp.net application

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.

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

C# Windows Form Application - Send email using gmail smtp

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

Categories

Resources