Furkan Gözükara
Furkan Gözükara

Reputation: 23830

How to set from when sending email via smtp server with using microsoft c#

How to set "from" when sendin email via smtp server with using microsoft c#

if you look this image you will understand what i mean

i use the code below for sending emails

alt text

MailMessage mail = new MailMessage();
                    mail.To.Add(srUserEmail);

                    string srBody = "bla bla bla";
 mail.From = new MailAddress("[email protected]");
                        mail.Subject = "bla bla bla";
     mail.Body = srBody;
                    mail.IsBodyHtml = true;

                    SmtpClient smtp = new SmtpClient();

                    smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                    smtp.UseDefaultCredentials = true;
                    smtp.Host = "xxx.xx.xx.xx";
                    smtp.Port = xxx;


                    smtp.Send(mail);

Upvotes: 0

Views: 518

Answers (2)

SLaks
SLaks

Reputation: 887443

Pass a second argument to the MailAddress constructor:

mail.From = new MailAddress("[email protected]", "Some Display Name")

Upvotes: 7

StriplingWarrior
StriplingWarrior

Reputation: 156524

Format your email address like this:

mail.From = new MailAddress("PokemonCraft <[email protected]>");

The MailAddress object should recognize that the part inside <> tags is an email address, whereas the part preceding that is the name of the person or business sending the email.

Upvotes: 1

Related Questions