Set "From" address when using System.Net.Mail.MailMessage? - c#

I'm trying to send a password reset email, but I'm having trouble figuring out how to specify the sender's address.
Here's what I'm trying to do:
MailMessage mail = new MailMessage();
mail.From.Address = "support#mycompany.com";
mail.To.Add(Email);
mail.Subject = "Forgot Password";
mail.Body = "Click here to reset your password.";
SmtpClient smtp = new SmtpClient();
smtp.SendAsync(mail, null);
I'm sure it's possible, so how can I accomplish this in ASP.Net?

It turns out I was getting ahead of myself.
Removing Address from mail.From.Address allowed me to set the value, but needed the type MailAddress.
Here's the solution:
MailMessage mail = new MailMessage();
mail.From = new MailAddress("support#mycompany.com");
mail.To.Add(Email);
mail.Subject = "Forgot Password";
mail.Body = "Click here to reset your password.";
SmtpClient smtp = new SmtpClient();
smtp.SendAsync(mail, null);

Related

c# MailMessage From email address display name is not working

I tried to send an email and set display name of the from Email address. But when i received email in my client for instance gmail. Instead of display name i see email address without domain means From Email "FromEmail#abc.com" then when i received it shows me i received email from "FromEmail".
Here is my code
MailMessage mail = new MailMessage();
mail.BodyEncoding = System.Text.Encoding.UTF8;
mail.HeadersEncoding = System.Text.Encoding.UTF8;
mail.SubjectEncoding = System.Text.Encoding.UTF8;
mail.Priority = System.Net.Mail.MailPriority.High;
mail.IsBodyHtml = true;
mail.From = new MailAddress("FromEmail#abc.com","Automate");
mail.To.Add(new MailAddress("ToEmail#abc.com"));
mail.Subject = "This is test email";
mail.Body = "This is test mail.";
SmtpClient client = new SmtpClient("smtp.office365.com");
client.EnableSsl = true;
client.Credentials = new System.Net.NetworkCredential("senderEmail#abc.com", "*****");
client.Send(mail);

.Net email integration won't send using godaddy SMTP Credential

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

Sending outlook mails with adding followup remainders in asp.net c#

while sending mail Adding remainders possible in outlook through asp.net c#
CODE:-
MailMessage msg = new MailMessage();
msg.To.Add("emailid");
msg.From = new MailAddress("fromid");
msg.IsBodyHtml = true;
msg.Body = "";
msg.Subject = "Alert!";
SmtpClient smtp = new SmtpClient();
smtp.Host = "host id";
msg.Priority = MailPriority.High;
msg.Headers.Add("X-Message-Flag", "For Your Information");
msg.Headers.Add("Reply-By", "08/06/2015 13:05:00"); smtp.Send(msg);
I think you mean reminder not remainder.
Please see below for what you would need to add to your code
DateTime replyDate = DateTime.Now.AddHours(4);
MailMessage msg = new MailMessage();
msg.Headers.Add("X-Message-Flag", "Follow up");
msg.Headers.Add("Reply-By", String.Format("{dd/MM/yyyy HH:mm:ss}", replyDate));

How create window to send email?

I want to create a button and when user click on that, a window form will open and the From is default text, "To" is also load from code behind and user can edit that text, "Content" is default text and user can edit too.
So now I can send email with:
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
SmtpClient SmtpServer = new SmtpClient("gw1.scei.a-star.edu.sg");
mail.From = new MailAddress("mydefaultemail");
mail.To.Add("the To emails will be input here");
mail.Subject = "Test Mail";
mail.Body = "This is for testing SMTP mail.";
SmtpServer.Credentials = new System.Net.NetworkCredential("mydefaultemail", "");
SmtpServer.Send(mail);
Now I don't know how could I make it to be wildows form and catch the text in that form to input into this code?
You add a textbox to your Windows form.
Then, in your code, you get the text property value of that textbox and set your email variable accordingly.
mail.Body = myTextBox.Text;
SmtpClient client = new SmtpClient("smtp.gmail.com");
client.Port = 587;
client.EnableSsl = true;
client.Timeout = 100000;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential(
"yourid#gmail.com", "yourgmailpassword");
MailMessage msg = new MailMessage();
msg.To.Add("Send To email Id");
msg.From = new MailAddress("yourid#gmail.com");
msg.Subject ="Subject";
msg.Body = "Message";
client.Send(msg);

Send mail smtp gmail from address showing the gmail address

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.

Categories