In C# I have a method that sends an email via a gmail account.
When I open the email in microsoft outlook the from address is shown as the gmail address and not the strFromAddress that I use in the headers.
SmtpClient smtp = new SmtpClient();
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.UseDefaultCredentials = true;
smtp.EnableSsl = true;
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.Credentials = new NetworkCredential("***#gmail.com", "*****");
var strFromAddress = "no-repl#demuynck-printing.be";
var strToAddress = "sander#demuynck-media.be";
var strSubject = "Album: '" + lbltitel.Text + "' bestelling";
var strBody = "<html><head>";
// new instance of MailMessage
MailMessage mailMessage = new MailMessage();
// Sender Address
mailMessage.From = new MailAddress(strFromAddress);
// mailMessage.Headers("Selexion Clix Demuynck <no-reply#demuynck-printing.be>");
// mailMessage.Bcc.Add(new MailAddress("no-reply#demuynck-printing.be"));
// Recepient Address
mailMessage.To.Add(new MailAddress(strToAddress));
mailMessage.Headers.Add("Reply-To", "info#demuynck-printing.be");
// Subject
mailMessage.Subject = strSubject;
// Body
mailMessage.Body = strBody;
// format of mail message
mailMessage.IsBodyHtml = true;
// new instance of Smtpclient
smtp.Send(mailMessage);
Just set the displayname property on the MailAddress like so:
MailAddress fromAddress = new MailAddress("user#domaina.com","no-reply#domainb.com");
GMail will change the 'from' address to the account used to login to the SMTP server, unless the email-address used in 'from' field is verified to belong to the same owner. So, in your gmail preferences, just add and verify this specific from address.
This happens because Google is violating the SMTP protocols. There is a detailed article about it here: http://lee-phillips.org/gmailRewriting/
Steve's comment is incorrect; an authenticated sender should be able to set any From: header.
Related
I am using SmtpClient to send email like this
MailMessage mail = new MailMessage("email-from#gmail.com", "email-to#gmail.com", "Contact from " + emailFrom, text);
SmtpClient client = new SmtpClient();
client.Port = 587;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential(emailAccount, accountPass);
client.Host = "smtp.gmail.com";
client.EnableSsl = true;
client.Send(mail);
This is working fine, I am receiving emails but I receive email from emailAccount address variable. I want to recieve emails from the 'From' field of message i.e email-from#gmail.com
Is it possible?
You can do something like these.
mail.From = fromEmailAddress; // Your variable that contains from address
for more details see.
https://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage(v=vs.110).aspx
Sending Email to multiple Recipients with MailMessage
http://www.c-sharpcorner.com/article/smtpmail-and-mail-message-send-mails-in-net/
How to send email through SMTP to email address which contain non-ASCII word.
Consider an users with email id : téést#test.com
Code is not throwing any exception and while checking the delivery report it gave an error message:
"Technical details of permanent failure:
local-part of envelope RCPT address contains utf8 but remote server did not offer SMTPUTF8 ".
I am using .net 4.5 and my code sample is :
MailMessage message = new MailMessage();
message.From="test#test.com";
message.Subject = "subject";
message.IsBodyHtml = true;
message.Body = "body";
message.To.Add("téést#test.com");
SmtpClient smtp = new SmtpClient();
smtp.Host = "SMTPServer";
smtp.Port = "SMTPPort";
var userName = "SMTPUserName";
var password = "SMTPPassword";
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential(userName, password);
smtp.EnableSsl = true;
smtp.DeliveryFormat = SmtpDeliveryFormat.International;
smtp.Send(message);
Thanks In Advance.
Hi its been almost a day that I've been figuring things out with regards to sending email from godaddy email account to a gmail account. I have had my research online and almost tried everything but no luck .. here's what I made so far.
protected void generateEmail(){
MailMessage mail = new MailMessage ();
mail.From = new System.Net.Mail.MailAddress ("contact#company.com");
// The important part -- configuring the SMTP client
SmtpClient smtp = new SmtpClient ();
smtp.Port = 465; // [1] You can try with 465 also, I always used 587 and got success
smtp.EnableSsl = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network; // [2] Added this
smtp.UseDefaultCredentials = false; // [3] Changed this
smtp.Credentials = new NetworkCredential ("contact#company.com", "password123!"); // [4] Added this. Note, first parameter is Email Address of the sender and the next parameter is the password.
smtp.Host = "relay-hosting.secureserver.net";
//recipient address
mail.To.Add (new MailAddress ("test#gmail.com"));
mail.To.Add (new MailAddress ("testagain#gmail.com"));
//Formatted mail body
mail.IsBodyHtml = true;
string st = "This is a Test Message";
mail.Body = st;
smtp.Send (mail);
}
Can anyone help me out ? would appreciate any hands..
protected void generateEmail()
{
//Create the msg object to be sent
MailMessage msg = new MailMessage();
//Add your email address to the recipients
msg.To.Add("whereEmailWillBeSent#gmail.com");
//Configure the address we are sending the mail from
MailAddress address = new MailAddress("mail#company.com");
msg.From = address;
msg.Subject ="Hi this is mail from company";
msg.Body = "Your Message";
SmtpClient client = new SmtpClient();
//for Godaddy
client.Host = "relay-hosting.secureserver.net";
client.Port = 25;
client.EnableSsl = false;
client.UseDefaultCredentials = false;
//Send the msg
client.Send(msg);
//Display some feedback to the user to let them know it was sent
}
}
I have tried sending mail from gmail and other share hosting mail server and its works as expected
But Now my requirement is sending mail from windows mail server.whenever i try to send mail it shows gives me error failure sending mail.
SmtpClient client = new SmtpClient();
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Host = "us2.webmail.mailhostbox.com";
string fromaddress = "info#csi.com";
client.Port = 25;
//client.Host = "mail.csisite.com";
// setup Smtp authentication
System.Net.NetworkCredential credentials =
new System.Net.NetworkCredential("info#csisite.com", "password");
//client.UseDefaultCredentials = true;
client.Credentials = credentials;
client.EnableSsl = false;
try
{
//mail For Customer
MailMessage msgcustomer = new MailMessage();
msgcustomer.From = new MailAddress(fromaddress);
msgcustomer.To.Add(new MailAddress(EmailID));
msgcustomer.Subject = "Thank you for writing to us" ;
msgcustomer.IsBodyHtml = true;
msgcustomer.Body = "Test Mail";
client.Send(msgcustomer)
Send Mail:
var smtp = new System.Net.Mail.SmtpClient();
{
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
smtp.Timeout = 20000;
}
// Passing values to smtp object
smtp.Send(fromAddress, toAddress, subject, body);
using System.Net.Mail
MailMessage msgMail = new MailMessage();
MailMessage myMessage = new MailMessage();
myMessage.From = new MailAddress("sender's email","sender`s name and surname");
myMessage.To.Add("recipient's email");
myMessage.Subject = "Subject";
myMessage.IsBodyHtml = true;
myMessage.Body = "Message Body";
SmtpClient mySmtpClient = new SmtpClient();
System.Net.NetworkCredential myCredential = new System.Net.NetworkCredential("email", "password");
mySmtpClient.Host = "your smtp host address";
mySmtpClient.UseDefaultCredentials = false;
mySmtpClient.Credentials = myCredential;
mySmtpClient.ServicePoint.MaxIdleTime = 1;
mySmtpClient.Send(myMessage);
myMessage.Dispose();
updated user is getting the following error
{"A socket operation was attempted to an unreachable network 208.91.199.230:25"} and Error Code is 10051
The Error 10051 is a socket error that appears when the target network or host server can not be reached. The error could have occurred due to an erroneous configuration of the router, Internet disconnection, or a blocking action by a firewall. This error has occurred mostly in attempts to generate Internet Control Message Protocol (ICMP) and Simple Mail Transfer Protocol (SMTP) connections with wrong configurations. This error also indicates that the Windows software employed is configured to communicate to a router, since the 10065 error appears instead if Windows is not set to link to a router.
you should make ensure that the network path is correct, that the computer is not running two software firewalls and that the targeted computer is not turned off.
Use This Code for sending mail this is tested code.
public static void SendMailMessage(string from, string to, string bcc, string cc, string subject, string body)
{
// Instantiate a new instance of MailMessage
MailMessage mMailMessage = new MailMessage();
// Set the sender address of the mail message
mMailMessage.From = new MailAddress(from);
// Set the recepient address of the mail message
mMailMessage.To.Add(new MailAddress(to));
// Check if the bcc value is null or an empty string
if ((bcc != null) && (bcc != string.Empty))
{
// Set the Bcc address of the mail message
mMailMessage.Bcc.Add(new MailAddress(bcc));
} // Check if the cc value is null or an empty value
if ((cc != null) && (cc != string.Empty))
{
// Set the CC address of the mail message
mMailMessage.CC.Add(new MailAddress(cc));
} // Set the subject of the mail message
mMailMessage.Subject = subject;
// Set the body of the mail message
mMailMessage.Body = body;
// Set the format of the mail message body as HTML
mMailMessage.IsBodyHtml = true;
// Set the priority of the mail message to normal
mMailMessage.Priority = MailPriority.Normal;
// Instantiate a new instance of SmtpClient
SmtpClient mSmtpClient = new SmtpClient();
// Send the mail message
mSmtpClient.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
mSmtpClient.Credentials = new System.Net.NetworkCredential
("YourEmail#gmail.com", "Password");
//Or your Smtp Email ID and Password
mSmtpClient.EnableSsl = true;
mSmtpClient.Send(mMailMessage);
}
Note: I have been trying to send email through my ISP mail server with no success - following all the suggestions that I could find on these forums. I just discovered that it would work fine if I did not attempt to alter the default credentials setting. If I included either: client.UseDefaultCredentials = true; or client.UseDefaultCredentials = false; in my code, then I would get a login failure message from the server. By not including either, it worked fine.
I am getting following error message while sending email using gmailD.
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.
MailMessage objMailMessage = new MailMessage();
objMailMessage.From = new MailAddress("suraj.podval#in.vsolutions.com");
objMailMessage.To.Add(new MailAddress("itslaxman#gmail.com"));
objMailMessage.Subject = "Test";
objMailMessage.Body = "Test Test";
objMailMessage.IsBodyHtml = true;
SmtpClient smtpClient = new SmtpClient();
smtpClient.Host = "smtp.gmail.com";
smtpClient.Port = 587;
smtpClient.EnableSsl = true;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new System.Net.NetworkCredential("user#gmail.com", "password");
smtpClient.Send(objMailMessage);
Try changing the port to 465
SmtpMail oMail = new SmtpMail("TryIt");
SmtpClient oSmtp = new SmtpClient();
// Your gmail email address
oMail.From = "gmailid#gmail.com";
// Set recipient email address
oMail.To = "support#emailarchitect.net";
// Set email subject
oMail.Subject = "test email from gmail account";
// Set email body
oMail.TextBody = "this is a test email sent from c# project with gmail.";
// Gmail SMTP server address
SmtpServer oServer = new SmtpServer("smtp.gmail.com");
// If you want to use direct SSL 465 port,
// please add this line, otherwise TLS will be used.
// oServer.Port = 465;
// detect SSL/TLS automatically
oServer.ConnectType = SmtpConnectType.ConnectSSLAuto;
// Gmail user authentication
// For example: your email is "gmailid#gmail.com", then the user should be the same
oServer.User = "gmailid#gmail.com";
oServer.Password = "yourpassword";
check below link: http://www.emailarchitect.net/easendmail/kb/csharp.aspx?cat=2