I'm trying to set up an automatic email sender.
My smtp server requires authentication. I'm not using ssl but still I'm getting the following response from server:
The server response was: 5.7.1 You are not authorized, authentication is required.
And if I check on the server it got an empty username.
Here is my code:
public void email_send(string mailadress,string docname)
{
string host = "myhost";
string username = "user";
string password = "password";
int port = 625;
var nc = new NetworkCredential(username, password);
var auth = nc.GetCredential(host,port,"Basic");
using (var mail = new MailMessage())
using (var SmtpServer = new SmtpClient())
{
SmtpServer.Host = host;
SmtpServer.Port = port;
SmtpServer.Credentials = auth;
mail.From = new MailAddress("fromadress");
mail.To.Add(mailadress);
mail.Subject = "Subject";
mail.Body = "Body";
Attachment attachment;
attachment = new Attachment("D:/" + docname + ".pdf");
mail.Attachments.Add(attachment);
mail.IsBodyHtml = true;
SmtpServer.DeliveryFormat = SmtpDeliveryFormat.International;
SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network;
SmtpServer.Send(mail);
}
}
Related
This is my mail sending code.....
string fromAddress = "from#abc.gov.in";
string toAddress = "darpansamani#gmail.com";
string fromPassword = "*********";
string subject = "Citizens Forum";
string body = "Demo Body";
MailMessage mail = null;
using (mail = new MailMessage(new MailAddress(fromAddress), new MailAddress(toAddress)))
{
mail.Subject = subject;
mail.Body = body;
mail.To.Add(toAddress);
SmtpClient smtpMail = null;
using (smtpMail = new SmtpClient("smtp.gmail.com"))
{
smtpMail.Port = 587;
smtpMail.EnableSsl = false;
smtpMail.Credentials = new NetworkCredential(fromAddress, fromPassword);
smtpMail.UseDefaultCredentials = false;
smtpMail.Send(mail);
Console.WriteLine("sent");
Console.ReadLine();
}
}
This code give me error of "Unable to read data from the transport connection: net_io_connectionclosed."
i am using asp.net with c# website and cant send mail to my mail address please help me
Thank you
When I send mail to my gmail account, it shows below error.
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.1 Authentication required...
code I am using is below
MailMessage m = new MailMessage();
SmtpClient sc = new SmtpClient();
try
{
m.From = new MailAddress("me#gmail.com");
m.To.Add("me#gmail.com");
m.Subject = "This is a Test Mail";
m.IsBodyHtml = true;
m.Body = "test gmail";
sc.Host = "smtp.gmail.com";
sc.Port = 587;
sc.Credentials = new System.Net.NetworkCredential("me#gmail.com", "passward");
sc.UseDefaultCredentials = true;
sc.EnableSsl = true;
sc.Send(m);
Response.Write("Email Send successfully");
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
Just tried your code, had to fiddle with a couple things but was sent this. Funny because I have done this previously using Gmail smtp (couple years back). But it looks like they are now verifying apps that use their platform.
Either use another smtp server that you are signed up to, or use your own. (there must be a test one that is available online??). Pretty sure sendgrid do a free trial.
using System.Net;
using System.Net.Mail;
string smtpAddress = "smtp.mail.yahoo.com";
int portNumber = 587;
bool enableSSL = true;
string emailFrom = "email#yahoo.com";
string password = "abcdefg";
string emailTo = "someone#domain.com";
string subject = "Hello";
string body = "Hello, I'm just writing this to say Hi!";
using (MailMessage mail = new MailMessage())
{
mail.From = new MailAddress(emailFrom);
mail.To.Add(emailTo);
mail.Subject = subject;
mail.Body = body;
mail.IsBodyHtml = true;
// Can set to false, if you are sending pure text.
mail.Attachments.Add(new Attachment("C:\\SomeFile.txt"));
mail.Attachments.Add(new Attachment("C:\\SomeZip.zip"));
using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber))
{
smtp.Credentials = new NetworkCredential(emailFrom, password);
smtp.EnableSsl = enableSSL;
smtp.Send(mail);
}
}
Please try this this should work for you
Thank you
Trying to send an email through an SMTP server, but I'm getting a nondescript error on the smtp.Send(mail); snippet of code.
Server side, the relay IP addresses look correct. Scratching my head about what I'm missing.
MailMessage mail = new MailMessage();
mail.To.Add(txtEmail.Text);
mail.From = new MailAddress("no-reply#company.us");
mail.Subject = "Thank you for your submission...";
mail.Body = "This is where the body text goes";
mail.IsBodyHtml = false;
SmtpClient smtp = new SmtpClient();
smtp.Host = "mailex.company.us";
smtp.Credentials = new System.Net.NetworkCredential
("AdminName", "************");
smtp.EnableSsl = false;
if (fileuploadResume.HasFile)
{
mail.Attachments.Add(new Attachment(fileuploadResume.PostedFile.InputStream,
fileuploadResume.FileName));
}
smtp.Send(mail);
Try adding smtp.DeliveryMethod = SmtpDeliveryMethod.Network; prior to send.
For reference, here is my standard mail function:
public void sendMail(MailMessage msg)
{
string username = "username"; //email address or domain user for exchange authentication
string password = "password"; //password
SmtpClient mClient = new SmtpClient();
mClient.Host = "mailex.company.us";
mClient.Credentials = new NetworkCredential(username, password);
mClient.DeliveryMethod = SmtpDeliveryMethod.Network;
mClient.Timeout = 100000;
mClient.Send(msg);
}
Typically called something like this:
MailMessage msg = new MailMessage();
msg.From = new MailAddress("fromAddr");
msg.To.Add(anAddr);
if (File.Exists(fullExportPath))
{
Attachment mailAttachment = new Attachment(fullExportPath); //attach
msg.Attachments.Add(mailAttachment);
msg.Subject = "Subj";
msg.IsBodyHtml = true;
msg.BodyEncoding = Encoding.ASCII;
msg.Body = "Body";
sendMail(msg);
}
else
{
//handle missing attachments
}
var client = new SmtpClient("smtp.gmail.com", 587);
Credentials = new NetworkCredential("sendermailadress", "senderpassword"),
EnableSsl = true
client.Send("sender mail adress","receiver mail adress", "subject", "body");
MessageBox.Show("mail sent");
This is Adil's answer formatted for C#:
public static class Email
{
private static string _senderEmailAddress = "sendermailadress";
private static string _senderPassword = "senderpassword";
public static void SendEmail(string receiverEmailAddress, string subject, string body)
{
try
{
var client = new SmtpClient("smtp.gmail.com", 587)
{
Credentials = new NetworkCredential(_senderEmailAddress, _senderPassword),
EnableSsl = true
};
client.Send(_senderEmailAddress, receiverEmailAddress, subject, body);
}
catch (Exception e)
{
Console.WriteLine("Exception sending email." + Environment.NewLine + e);
}
}
}
You didn't specify the port.
smtp.Port = 1111; // whatever port your SMTP server uses
The SMTP has three different "standard" ports: 25, 465 and 587. According to msdn documentation, the default value for the Port property is 25.
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);
I'm developing a simple send mail app in C#, using my CMail Server:
MailMessage mail = new MailMessage("from#mail.com", "destination#mail.com");
mail.Subject = "Sub";
mail.Body = "Hi!";
SmtpClient smtp = new SmtpClient("MyServer");
System.Net.NetworkCredential cred = new System.Net.NetworkCredential("user", "pass");
smtp.UseDefaultCredentials = false;
smtp.Credentials = cred;
smtp.Send(mail);
Obviously i ommited my account information, so, this code throws me an Authentication Exception for some reason. I first thought that the code was wrong, so i change the info to my gmail account and everything goes fine, with the only SMTP server that i having trouble is with the CMail. Is there a problem with .NET and CMail's SMTP ?
Thanks for the help and comments!
Try adding:
smtp.EnableSsl = true;
If you are using 2-step verification then you will need to add application specific password.
Full work sample
public static void sendEmail()
{
//for use GMAIL require enable -
//https://myaccount.google.com/lesssecureapps?rfn=27&rfnc=1&eid=39511352899709300&et=0&asae=2&pli=1
Console.WriteLine("START MAIL SENDER");
//Авторизация на SMTP сервере
SmtpClient Smtp = new SmtpClient("smtp.gmail.com", 587);
Smtp.EnableSsl = true;
Smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
Smtp.UseDefaultCredentials = false;
//username password
Smtp.Credentials = new NetworkCredential("rere", "rere");
//Формирование письма
MailMessage Message = new MailMessage();
Message.From = new MailAddress("rere#gmail.com");
Message.To.Add(new MailAddress("rere#gmail.com"));
Message.Subject = "test mesage";
Message.Body = "tttt body";
string file = "D:\\0.txt";
if (file != "")
{
Attachment attach = new Attachment(file, MediaTypeNames.Application.Octet);
ContentDisposition disposition = attach.ContentDisposition;
disposition.CreationDate = System.IO.File.GetCreationTime(file);
disposition.ModificationDate = System.IO.File.GetLastWriteTime(file);
disposition.ReadDate = System.IO.File.GetLastAccessTime(file);
Message.Attachments.Add(attach);
Console.WriteLine("ADD FILE [" + file + "]");
}
try
{
Smtp.Send(Message);
MessageBox.Show("SUCCESS");
}
catch { MessageBox.Show("WRONG"); }
}