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.
Related
I'm working on a project that needs to send an invitation email to an email specified by the user.
I'm using C#, SMTP and the body is an html template.
If I try, Locally there is no problem, I can send emails and everything works as intended, I receive the email with the correct template.
The problem comes when I'm trying to send the email from the productive environment, from there the email received is with the empty body and no traces of the body template.
This is the code I'm using to send the email.
MailMessage msg = new MailMessage(Configuration["EmailFrom"],to);
msg.Subject = subject;
msg.Body = this.emailBody;
msg.IsBodyHtml = true;
msg.BodyEncoding = System.Text.Encoding.UTF8;
SmtpClient smtpClient = new SmtpClient(Configuration["SMTP"], Convert.ToInt32(Configuration["PORT"]));
System.Net.NetworkCredential credentials = new System.Net.NetworkCredential(Configuration["EmailFrom"], Configuration["PasswordEmailFrom"]);
smtpClient.Credentials = credentials;
smtpClient.EnableSsl = true;
smtpClient.Send(msg);
Now, I know the productive enviroment is hosted in a AWS server, but don't know the configuration of the server or if it has something to do with the problem.
The templates are using divs instead of tables in some cases but i think is an unrelated issue, so I really don't know where the problem could be.
Edit:
I grab the info i will use to replace from the database and grabbing the template from a local HTML file hosted in the server.
This is the code where it sets the email template and replace the content:
EmailService sendDriverInvitation = new EmailService(_env);
sendDriverInvitation.setEmailTemplate($"ClientApp/{AssetsDirectory}/assets/emailTemplates/InvitationDriver.html");
sendDriverInvitation.emailBody = sendDriverInvitation.emailBody.Replace("[DER Name]", userName).Replace("[Company Name]", DbaName);
sendDriverInvitation.sendMail(email, "Invitation Email");
And this is the code for setEmailTemplate:
HTMLReader reader = new HTMLReader();
string htmlCompleteTemplatePath = Path.Combine(webRoot, htmlTemplatePath);
this.emailBody = reader.ReadHtmlFile(htmlCompleteTemplatePath);
I have created a webform in visual studio. This application is sending an email.
BUT I want the FROM address to be the logged in user's email. So for example, if John is logged in with his email and password and he clicks submit, the email will send to Person X but the from address will come from John. I hope this is a clear enough explanation.
This is the code snippet of my sending mail method.
{
MailMessage message = new MailMessage();
message.To.Add(nameddl.Text);
message.From = new MailAddress("");
message.Subject = "Notification";
message.Body = "An new entry has been made that requires your attention.";
message.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
}
So this line, message.From = new MailAddress(User.LoggedIn); this is what I want. BUT how do one code it to work like that, can it be made possible?
NB: This is NOT a MVC Application, just a regular ASP.NET Web form.
**EDIT Still struggling with this one, so if anyone got an idea that would be great?
If you are storing email in session then you can directly pass it like.
message.From = new MailAddress(Session["Email"].Tostring);
if this is not the case and you have stored email in database then you can get it from there by simply making one method that return email address.like
message.From = GetEmail();
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
I am currently trying to get a debug mail up and running. The moment an error occurs it will send a mail with the error to the mail i use. But after letting somebody test it he actually got my mail password and mail out of it and decided to change the password.
public void Send() {
MailMessage MailMesaji = new MailMessage();
MailMesaji.Subject = "subject";
MailMesaji.Body = "mail body";
MailMesaji.From = "sender mail adress";
this.MailMesaji.To.Add(new MailAddress("to mail adress"));
System.Net.Mail.SmtpClient Smtp = new SmtpClient();
Smtp.Host = "smtp.gmail.com"; // for example gmail smtp server
Smtp.EnableSsl = true;
Smtp.Credentials = new System.Net.NetworkCredential("account name", "password");
Smtp.Send(MailMesaji);
}
So i was wondering, is it possible to encrypt the account name and password to prevent stealing ?
I am sorry if i did not search good enough, but could not find anything on how to encrypt email/password
As you need to recover the original password to use for the mail send, you would have to use some form reversible encryption.
It sounds like you are in a situation where you want to pass on your source code to another user to test. That tester will be able to simply breakpoint your code on the new System.Net.NetworkCredential line and see what is being passed to the constructor.
So, however you manage to encrypt your credentials, if you are passing the code (or executable) to somebody else for testing, then they will be able to access your password.
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