I am using Windows Form Application which is sending HTML emails using MailDefinition Class.
MailDefinition mailDefinition = new MailDefinition();
mailDefinition.BodyFileName = strfilename;
mailDefinition.From = "email#company.com";
Is it possible that somehow I can add a different email id where user will reply. I don't want the users to reply on this email#company.com email id.
I am looking for some thing like this:
mail.Headers.Add( "Reply-To", "alternate_email#mycompany.com")
I use the System.Net.Mail.SmtpClient class which has a send method that takes a System.Net.Mail.MailMessage parameter.
In the MailMessage class it has: replyTo and replyToList properties.
Add HTML content like this:
System.Net.Mail.MailMessage m = new System.Net.Mail.MailMessage();
m.Body = "HTML Content";
m.IsBodyHtml = true;
Related
The official Gmail API documentation is horrendous. Not getting any clue to integrate Gmail API using .NET framework in vs2017. I wanted to send the input data of the Web form to a user's email.
It would be a three step process -
Define an HTML template which which describes how your mail should be presented.
Write a small c# code to replace all place holders like your form fields , user name, etc.
private string createEmailBody(string userName, string title, string message)
{
string body = string.Empty;
//using streamreader for reading my htmltemplate
using(StreamReader reader = new StreamReader(Server.MapPath("~/HtmlTemplate.html")))
{
body = reader.ReadToEnd();
}
body = body.Replace("{UserName}", userName); //replacing the required things
body = body.Replace("{Title}", title);
body = body.Replace("{message}", message);
//// Instead of message add you own parameters.
return body;
}
When form is submitted, call step 2 code first. Then use it's output to set mail body.
Code would be something like -
string smtpAddress = "smtp.gmail.com";
int portNumber = 587;
bool enableSSL = true;
/// This mail from can just be a display only mail I'd
string emailFrom = "no-reply#gmail.com";
string subject = "your subject";
string body = createEmailBody();
using (MailMessage mail = new MailMessage())
{
mail.From = new MailAddress(emailFrom);
mail.To.Add(emailTo);
/// Add more to IDs if you want to send it to multiple people.
mail.Subject = subject;
mail.Body = body;
mail.IsBodyHtml = true;
// This is required to keep formatting of your html contemts
/// Add attachments if you want, this is optional
mail.Attachments.Add(new Attachment(yourfilepath));
using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber))
{
smtp.Credentials = new NetworkCredential(your-smtp-account-email, your-smtp-account-password);
smtp.EnableSsl = enableSSL;
smtp.Send(mail);
}
}
Refer this link for working example
https://www.c-sharpcorner.com/UploadFile/33b051/sending-mail-with-html-template/
EDIT: For using GMail API
Using GMAIL APIs you will need two nuget packages:
1. Install-Package Google.Apis.Gmail.v1
2. Install-Package AE.Net.Mail
Code is very similar to what we have for normal SMTP mail send. It is explained at: http://jason.pettys.name/2014/10/27/sending-email-with-the-gmail-api-in-net-c/
I want to send simple email with no attachment using default email application.
I know it can be done using Process.Start, but I cannot get it to work. Here is what I have so far:
string mailto = string.Format("mailto:{0}?Subject={1}&Body={2}", "to#user.com", "Subject of message", "This is a body of a message");
System.Diagnostics.Process.Start(mailto);
But it simply opens Outlook message with pre-written text. I want to directly send this without having user to manually click "Send" button. What am I missing?
Thank you
You need to do this :
string mailto = string.Format("mailto:{0}?Subject={1}&Body={2}", "to#user.com", "Subject of message", "This is a body of a message");
mailto = Uri.EscapeUriString(mailto);
System.Diagnostics.Process.Start(mailto);
I'm not sure about Process.Start() this will probably always only open a mail message in the default Mail-App and not send it automatically.
But there may be two alternatives:
Send directly via SmtpClient Class
using Outlook.Interop
You need to do this:
SmtpClient m_objSmtpServer = new SmtpClient();
MailMessage objMail = new MailMessage();
m_objSmtpServer.Host = "YOURHOSTNAME";
m_objSmtpServer.Port = YOUR PORT NOS;
objMail.From = new MailAddress(fromaddress);
objMail.To.Add("TOADDRESS");
objMail.Subject = subject;
objMail.Body = description;
m_objSmtpServer.Send(objMail);
I have a code for sending email through code, its as follows:
string subject = "SecureEmail: URS Scheduler - ";
string body = #"Message: My Message";
try
{
SmtpClient sm = new SmtpClient();
MailMessage msg = new MailMessage();
//msg.SubjectEncoding.
sm.Host = "email.myhost.com";
//Add Sender
msg.From = new MailAddress("abc#myhost.com");
//Add reciepents
sendMailToUsers(msg, "pqr#myhost.com");
//send message
msg.IsBodyHtml = true;
msg.Subject = subject;
msg.Body = body;
sm.Send(msg);
I am able to send message but its not encrypted, its a plain Text.
When I go to my Outlook mail client and Send Mail with above recipients and body, and subject starting with "SecureEmail:", I get an Encrypted Email with a button "Open Message". When I click on Open Message it redirects me to https://web1.zixmail.net/s/e?b=domain&m=encrypted msg and other info,then I login into it and and able to see the plain text of mail body.
Please help me in getting above behaviour through my code.
Your company is using ZixMail, and your Outlook has a plugin to enable this. If you want to send ZixMail from C#, you'll need to use their toolset and API. Reffer to ZixMail documentation and support.
I want to send email with EWS using custom display name.
Here is my Code:
EmailMessage message = new EmailMessage();
message.From = new EmailAddress("someone","one#two.com");
My expectation is that the received email to come from: someone <one#two.com>
Instead of this I get the email from: one <one#two.com>
Anybody know what would be the problem here?
EDIT
Have you tried this?
new EmailAddress("someone <one#two.com>;");
PREVIOUS
System.Net.Mail Namespace has MailAddress class, not EmailAddress.
Are you using System.Net.Mail? And if not why not?
MailAddress Constructor (String, String) is this:
public MailAddress(
string address,
string displayName
)
so you should use this way:
message.From = new MailAddress("one#two.com","someone");
I have made an web application for sending e-mail. It works fine.
The problem is receiver end - Receiver shows NetworkCredential User Email as From Email.
And the email provided as From Email doesn't exist.
i want to show the suplied email not the networkcredential user email to the receiver.
sample code-
using System.Net.Mail;
MailMessage oMsg = new MailMessage();
oMsg.From = new MailAddress("sender#somewhere.com","Diplay Name");
oMsg.To.Add(new MailAddress("recipient#somewhere.com"));
oMsg.Subject = "Send Using Web Mail";
oMsg.Body ="Hi..";
System.Net.Mail.SmtpClient s = new System.Net.Mail.SmtpClient("host", port_no);
System.Net.NetworkCredential nc = new System.Net.NetworkCredential("user", "password");
s.EnableSsl = true;
s.UseDefaultCredentials = false;
s.Credentials = nc;
s.Send(oMsg);
The receiver gets from email is "user" but i want to show "sender#somewhere.com".
I think you need to update the Display name of the e-mail address that you send from.
Update oMsg.From = new MailAddress("sender#somewhere.com"); to be oMsg.From = new MailAddress("sender#somewhere.com","sender#somewhere.com");
MailAddress has an overload which allows you to pass is a display name for the given mail addres e.g. new MailAddress("sender#somewhere.com", "Display Name");
Some mail services (such as google), override the .FROM value, and will always use the ENVELOPE value, which is the NetworkCredential username.
I have a feeling that is what you are seeing.