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/
Related
I am using C#. I have a method in which the SMTP client is configured and an email is sent:
MailMessage mail = new MailMessage("myEmail#myDomain.com", "otherEmail#google.com");
mail.CC.Add("otherEmailInMyDomain#myDomain.com");
mail.Subject = "Normal Subject";
mailMessage.Body = "Normal Body";
SmtpClient client = new SmtpClient();
client.EnableSsl = true;
client.Port = 587;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = credentials;
client.Host = "smtp.office365.com";
client.Send(mailMessage);
The email is being received by the otherEmail#google.com (to which it is being sent directly), but is not being received by the otherEmailInMyDomain#myDomain.com.
This error only happens in the server, whenever i try this code in my machine, it works perfectly.
I developing an app which is an email generator, the thing is that I need to send a carbon copy of my email to the same email I am using to send the original one, basically I need to duplicate every email. But when I set the cc email the code simply ignores the CC.
C# Code:
const string fromPassword = "password";
string subject = "Subject " + sXmlArray[0].ToString();
SmtpClient smtp = new SmtpClient();
//Google SMTP Host
smtp.Host = "smtp.gmail.com";
//smtp.Host = "smtp.office365.com";
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Credentials = new NetworkCredential("myEmail#domain.com", fromPassword);
smtp.Timeout = 100000;
MailMessage message = new MailMessage(fromAddress, toAddress);
message.Subject = subject;
message.IsBodyHtml = true;
message.Body = StrContent.ToString();
//Carbon Copy
//HERE IS THE ISSUE
MailAddress CarbonCopy = new MailAddress("myEmail#domain.com");
message.CC.Add(CarbonCopy);
smtp.Send(message);
Why the code ignores this piece of code? If I declare a different email it works fine?
Can somebody help me out with this issue? Thanks in advance.
I am trying to send a mail the form posts, but when the form posts it tries to send the mail and then times out. the code that is timing out is this:
MailMessage mail = new MailMessage();
mail.From = new MailAddress("someone#example.com");
mail.To.Add("someoneElse#example.com");
SmtpClient smtp = new SmtpClient();
smtp.Port = 465;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.UseDefaultCredentials = false;
smtp.Host = "smtp.gmail.com";
mail.Subject = "Hello";
mail.Body = "World!";
smtp.Send(mail);
This is almost certainly an issue with your client setup.
If you're using port 465, you're required to use SSL -
See here https://support.google.com/a/answer/176600?hl=en
In your current client setup this is omitted.
Also, I think you're missing your credentials.
Try changing your client to
SmtpClient client = new SmtpClient();
client.Host = "smtp.gmail.com";
client.Port = 587;
client.EnableSsl = true; //ssl is required
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("your-email#gmail.com", "YourPassword");
client.Timeout = 20000; //increase the timeout
As mentioned in the comments, you may also need to allow "less secure apps" access to your account -
Info here http://support.google.com/accounts/answer/6010255
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
}
}
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.