From property in MailMessage - c#

var client = new SmtpClient("smtp.gmail.com", 587)
{
Credentials = new NetworkCredential("mail#gmail.com", "supersecretpassword"),
EnableSsl = true
};
MailMessage message = new MailMessage(new MailAddress(sender),
new MailAddress(recepient));
//message.From = new MailAddress(sender);
message.IsBodyHtml = true;
// message.To.Add(new MailAddress(recepient));
//message.ReplyToList.Add(new MailAddress(sender));
message.Subject = "subject";
message.Body = "title";
client.Send(message);
I'm sending an email using the code above, however, if the recepient decides to reply to the email, i want the to reply to the Adress supplied in the sender parameter, but when i receive the email, the From field gives the address of the mail#gmail.com provided in the smtp info.
I tried setting the replyto, replytolist and from properties in MailMessage, but it doesn't cahnge anything.
With the reply to i could see the sender address in "ReplyTo" in gmail, but the default receiver if i pressed reply is still mail#gmail.com
Am i not supposed to be able to change this for some reason, or am i missing something?
EDIT
i'm suspecting this has to do with using a temporary gmail as smtp server, but i can't find any confirmation to this.

ReplyTo is deprecated, ReplyToList is a correct way.
If you think this is a gmail related problem try with another smtp (for example sendgrid )
Keep in mind that gmail uses Sender and From differently in case of "fake" account as you can read here

https://stackoverflow.com/a/3872880/744610
according to this answer it is because i'm using gmail as smtp server, and gmail doesn't allow changing the From/sender properties of an email, to avoid spam

Related

Is it possible to send email from Google Apps from a Nickname in C#

I have a standard Google Apps account and they host my email for me..
Currently I can send emails in C# code, but they always come from my main email address.. I have a couple of nicknames setup... and in the Gmail interface, its possible as descibed here : Google Apps - Send email from a nickname
But I want to do this from code.. so I can send emails as "info#" or "support#"...
I have an appSetting called "EmailFrom" that is defined as "info#"...
MailMessage mm = new MailMessage(ConfigurationManager.AppSettings["EmailFrom"], ToAddress);
//'(2) Assign the MailMessage's properties
mm.Subject = Subject;
mm.Body = MessageBody;
mm.IsBodyHtml = true;
//'(3) Create the SmtpClient object
SmtpClient smtp = new SmtpClient();
smtp.Port = System.Convert.ToInt32(ConfigurationManager.AppSettings["smtpPort"]);
smtp.EnableSsl = true;
smtp.Send(mm);
Is that possible?
Ok..
Figured it out...
It would seem that I needed to add the email address in the gmail interface...
Select the Settings menu, and choose Accounts.... then select "Add another email address you own"..
I just went through the verification process adding in the nickname I wanted to send from..
Easy..

c# smtp get cookies

How I can get cookies in smtp?
My code
SmtpClient Smtp = new SmtpClient("smtp.mail.ru", 2525);
Smtp.Credentials = new NetworkCredential(dom_[0], dom[1]);
//Smtp.EnableSsl = false;
//Формирование письма
MailMessage Message = new MailMessage();
Message.IsBodyHtml = true;
Message.From = new MailAddress(dom[0]);
Message.To.Add(new MailAddress(mail));
Message.Subject = ch[0];
Message.Body = ch[1];
Smtp.Send(Message);//отправка
You don't.
Cookies are an HTTP concept, not a SMTP concept.
You get cookie values from the ASP.NET page or user control you are calling this code from, and pass them into the mail message. The mail message itself doesn't support cookies, but the page or user control calling it does.
I think you can try to do this by referencing an external resource ( an image for example ) but this will only make sense if the user opens the email in a browser and even then it might still be blocked by the browser or the browser email client.
Ether way i think it's a bad idea to try setting cookies from emails.

SMTP Exception when trying to send mail in C#

I am using the following very simple code and I keep getting System.Net.Mail.SmtpException failure sending mail. This is my code:
static void main(string[] args)
{
MailAddress from = new MailAddress("MyEmail#gmail.com", "Mr. Test");
MailAddress to = new MailAddress("AnotherEmail#gmail.com", "mr. man");
MailMessage msg = new MailMessage(from, to);
msg.Subject = "email";
msg.Body = "This is email.";
SmtpClient client = new SmtpClient("smtp.gmail.com");
client.Send(msg);
}
I have never tried programmatically sending email before so I appreciate the help.
You're missing credentials and sending as TLS (secured connection):
Credentials = new NetworkCredential("myusername#gmail.com", "mypwd"),
EnableSsl = true
More Details here:
Sending email through Gmail SMTP server with C#
First thing I see incorrect with the code given is there is no username/password given for validation with the smtp server.
Also, to get a better idea of what exactly is causing the SmtpException, catch the exception in your debugger and look at the details of the exception. I've gotten good explanations of what is causing SMTP errors by doing this.
You can try following these directions to send mail using SMTP via gmail. http://blogs.msdn.com/b/mariya/archive/2006/06/15/633007.aspx
Google does not want you to use port 25, they want you to use 587 (ssl) or 467. They also require that you authenticate when sending mail.

Using System.Net.Mail.SmtpClient, change the SMTP FROM email address

As it says in the title, I wish to change the FROM address provided to the SMTP server, as opposed to the FROM address in the email envelope.
The closest sample I can find is from Java, which is can be found here
Thanks
Bottom line is, you can't do this. The FROM address used in System.Net.Mail is used for both the SMTP transaction (Envelope-From) and the MailMessage from header value.
Sorry,
Dave
The FROM provided to the SMTP server is the login of the SmtpClient while the one in the Mail is the FROM in the MailMessage.
SmtpClient smtp = new SmtpClient();
smtp.Host = "myserver.address.com";
smtp.Credentials = new NetworkCredential("me#server.com", "myPassword");
MailMessage msg = new MailMessage();
msg.From = "otherMe#server.com";
//OTHER MESSAGE SETTINGS
smtp.Send(msg);
This should send an e-mail from "otherMe#server.com" using the authentication on the server for the user "me#server.com"
The Java example is about return address, not From.
As Far as I know, you can't do this. SMTP servers use the From address to decide if the want to relay or not.
The only other credential you've got is the Login to the SMTP server.
Unfortunately, this is not possible.
First there is a syntax error for smtp.Host = smtp.serv.com; This is not valid written string type, and the second thing is that the property Host doesn't exist.
As explained by bzlm, if you're using Network delivery method of SmtpClient, then you can set MAIL FROM by setting the MailMessage.Sender property. Note, however, that this will have the side-effect of adding a Sender heady to your message, which will cause many email clients to display present the message sender as "X on behalf of Y".

Sending an email with the header return-path using windows virtual mail server

I'm trying to send an email message using the .NET MailMessage class which can also have the return-path header added so that any bounces come back to a different email address. Code is below:
MailMessage mm = new MailMessage(
new MailAddress(string.Format("{0}<{1}>", email.FromName, email.FromEmail)),
new MailAddress(emailTo));
mm.Subject = ReplaceValues(email.Subject, nameValues);
mm.ReplyTo = new MailAddress(string.Format("{0}<{1}>", email.FromName, email.FromEmail));
mm.Headers.Add("Return-Path", ReturnEmail);
// Set the email html and plain text
// Removed because it is unneccsary for this example
// Now setup the smtp server
SmtpClient smtp = new SmtpClient();
smtp.Host = SmtpServer;
smtp.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
if (SmtpUsername.Length > 0)
{
System.Net.NetworkCredential theCredential =
new System.Net.NetworkCredential(SmtpUsername, SmtpPassword);
smtp.Credentials = theCredential;
}
smtp.Send(mm);
Whenever I check the email that was sent I check the header and it always seems to be missing return-path. Is there something I am missing to configure this correctly? As I said above I'm using the standard Virtual Mail Server on my development machine (XP) however it will run on Windows 2003 eventually.
Has anyone got any ideas why it isn't coming through?
The Return-Path is set based on the SMTP MAIL FROM Envelope. You can use the Sender property to do such a thing.
Another discussion on a related issue you will have sooner or later: How can you set the SMTP envelope MAIL FROM using System.Net.Mail?
And btw, if you use SmtpDeliveryMethod.PickupDirectoryFromIis, the Sender property is not used as a MAIL FROM; you have to use Network as a delivery method to keep this value.
I did not find any workaround for this issue.
PickupDirectoryFromIis, Sender property and SMTP MAIL FROM envelope

Categories