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();
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);
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 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..
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.