I am using MailMessage to send email from a web application. I believe I have the correct code in to "mask" the from name in the SendEmail() function
public static void SendEmail(string toAddress, string ccAddress, string bccAddress, string subject, string body, MemoryStream attachment, string attachmentName, bool isHtml, string fromAlias = "info#cipollainsurance.com")
{
using (MailMessage message = new MailMessage())
{
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(AddHeaderAndFooter(subject, body).ToString(), new System.Net.Mime.ContentType("text/html"));
var resources = LoadResources();
foreach (LinkedResource resource in resources)
{
htmlView.LinkedResources.Add(resource);
}
message.AlternateViews.Add(htmlView);
if (!string.IsNullOrEmpty(toAddress))
{
var addresses = toAddress.Split(',', ';');
foreach (string address in addresses)
{
message.To.Add(address);
}
}
if (!string.IsNullOrEmpty(ccAddress))
{
var ccAddresses = ccAddress.Split(',', ';');
foreach (string cc in ccAddresses)
{
message.CC.Add(cc);
}
}
if (!string.IsNullOrEmpty(bccAddress))
{
var bccAddresses = bccAddress.Split(',', ';');
foreach (string bcc in bccAddresses)
{
message.Bcc.Add(bcc);
}
}
if (!string.IsNullOrWhiteSpace(fromAlias))
{
message.Sender = new MailAddress("info#cipollainsurance.com", fromAlias, System.Text.Encoding.ASCII);
}
message.Body = body;
message.BodyEncoding = System.Text.Encoding.UTF8;
message.Subject = subject;
message.SubjectEncoding = System.Text.Encoding.UTF8;
message.IsBodyHtml = isHtml;
message.From = new MailAddress("info#cipollainsurance.com", "Cipolla Insurance");
if (attachment != null)
{
message.Attachments.Add(new Attachment(attachment, attachmentName));
}
SmtpClient client = new SmtpClient();
client.Send(message);
}
}
However the from email address still says email and email#webedgemedia.com which is what is specified in web.config.
<smtp from="info#cipollainsurance.com" deliveryMethod="Network">
<network host="smtp.gmail.com" enableSsl="true" port="587" password="myPassword" userName="email#webedgemedia.com" />
</smtp>
Can someone identify what I'm missing to send the email out as info#cipollainsurance.com?
I don't believe GMAIL allows you to do that - it makes it pretty trivial to use their services as a SPAM relay otherwise.
The most you can do is set a different REPLY-TO but your GMAIL (account) email is still FROM.
Related
how to specify folder path in the below code instead of default inbox folder.
how to send email to particular folder instead of inbox using SMTP c#
static string smtpAddress = "smtp.gmail.com";
static int portNumber = 587;
static bool enableSSL = true;
static string emailFromAddress = "xxxxx"; //Sender Email Address
static string password = "xxxxxxx"; //Sender Password
static string emailToAddress = "xxxx#yyy.vom"; //Receiver Email Address
static string subject = "Hello";
static string body = "Hello, This is Email sending test using gmail.";
public static void SendEmail()
{
using (MailMessage mail = new MailMessage())
{
mail.From = new MailAddress(emailFromAddress);
mail.To.Add(emailToAddress);
mail.Subject = subject;
mail.Body = body;
mail.IsBodyHtml = true;
//mail.Attachments.Add(new Attachment("D:\\TestFile.txt"));//--Uncomment this to send any attachment
using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber))
{
smtp.Credentials = new NetworkCredential(emailFromAddress, password);
smtp.EnableSsl = enableSSL;
smtp.Send(mail);
}
}
}
The SMTP protocol does not provide a way to send a message to a particular folder for any recipient.
I have a asp.net mvc site hosted in godaddy, but email sending is not working. In server code I writed the following
var emailmessage = new System.Web.Mail.MailMessage()
{
Subject = subject,
Body = body,
From = from,
To = to,
BodyFormat = MailFormat.Html,
Priority = MailPriority.High
};
SmtpMail.SmtpServer = "relay-hosting.secureserver.net";
SmtpMail.Send(emailmessage);
In web config I added the code
<system.net>
<mailSettings>
<smtp from="admin#flex.am">
<network host="relay-hosting.secureserver.net"/>
</smtp>
</mailSettings>
</system.net>
What can I do else?
Thanks!
Below snippet works for me. try this code and if it don't work for you, can you log whole exception message.
MailMessage Msg = new MailMessage();
// Sender e-mail address.
Msg.From = new MailAddress(txtEmail.Text);
// Recipient e-mail address.
Msg.To.Add("admin#abc.com");
//Msg.Subject = txtSubject.Text;
Msg.Body ="some body message";
SmtpClient smtp = new SmtpClient();
smtp.Host = "relay-hosting.secureserver.net";
smtp.Send(Msg);
var senderEmail = new MailAddress("Your email", "Your Name");
var receiverEmail = new MailAddress("Receiver email", "Receiver name");
var password = "your password"; //I used here APP password
var subject = "Subject";
var body = #"<html><body><p>Dear XYZ,</p></body></html>";
using (SmtpClient client = new SmtpClient
{
Host = "relay-hosting.secureserver.net",
Port = 25,
UseDefaultCredentials = false,
Credentials = new System.Net.NetworkCredential(senderEmail.Address, password)
})
using (var message = new MailMessage(senderEmail, receiverEmail)
{
Subject = subject,
Body = body,
IsBodyHtml = true
})
client.Send(message);
I am configuring SMTPClient with these codes to use local directory:
EmailHelper.cs
public bool SendMail(string from, string to, string cc, string subject, string body, bool isBodyHtml)
{
try
{
var smtpClient = new SmtpClient();
string pickUpFolder = #"C:\Users\kerem\Documents\Visual Studio 2012\Projects\Blog\Blog\Email";
smtpClient.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
Configuration configurationFile = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);
MailSettingsSectionGroup mailSettings = configurationFile.GetSectionGroup("system.net/mailSettings") as MailSettingsSectionGroup;
if (mailSettings != null)
{
pickUpFolder = mailSettings.Smtp.SpecifiedPickupDirectory.PickupDirectoryLocation;
smtpClient.PickupDirectoryLocation = pickUpFolder;
}
System.Net.Mail.MailMessage mailMessage = new System.Net.Mail.MailMessage();
mailMessage.From = new MailAddress(from);
mailMessage.To.Add(new MailAddress(to));
if (cc != "")
mailMessage.CC.Add(new MailAddress(cc));
mailMessage.Subject = subject;
mailMessage.Body = body;
mailMessage.IsBodyHtml = true;
smtpClient.Send(mailMessage);
return true;
}
catch (Exception err)
{
return false;
}
}
EmailHelper.cs in BlogServices Project in my Blog solution. Also there is a Blog Project in the same solution. I have replaced Blog\Blog\Email in pickUpFolder with Blog\Email and Blog\BlogServices\Email but I still have an error of Only absolute directories are allowed for pickup directory. Where is my mistake? Thanks in advance.
Add this to your project's web.config.
<system.net>
<mailSettings>
<smtp deliveryMethod="SpecifiedPickupDirectory">
<specifiedPickupDirectory pickupDirectoryLocation="C:\Users\kerem\Documents\Visual Studio 2012\Projects\Blog\Blog\Email\"/>
</smtp>
</mailSettings>
</system.net>
Alternatively, try deleting this line: pickUpFolder = mailSettings.Smtp.SpecifiedPickupDirectory.PickupDirectoryLocation;, but I suspect you'll have other problems if you go that route.
I have contact page on my website and I want users to enter Their name and email id (no password) and send email on button click.
I did Google and all solutions i am getting requires user password,
public static void SendMessage(string subject, string messageBody, string toAddress, string ccAddress, string fromAddress)
{
MailMessage message = new MailMessage();
SmtpClient client = new SmtpClient();
// Set the sender's address
message.From = new MailAddress(fromAddress);
// Allow multiple "To" addresses to be separated by a semi-colon
if (toAddress.Trim().Length > 0)
{
foreach (string addr in toAddress.Split(';'))
{
message.To.Add(new MailAddress(addr));
}
}
// Allow multiple "Cc" addresses to be separated by a semi-colon
if (ccAddress.Trim().Length > 0)
{
foreach (string addr in ccAddress.Split(';'))
{
message.CC.Add(new MailAddress(addr));
}
}
// Set the subject and message body text
message.Subject = subject;
message.Body = messageBody;
// smtp settings
var smtp = new System.Net.Mail.SmtpClient();
{
smtp.Host = "smtpHost";
smtp.Port =portno;
smtp.EnableSsl = true;
smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtp.Credentials = new NetworkCredential("fromEmail", "fromPassword");
smtp.Timeout = 20000;
}
// Send the e-mail message
smtp.Send(message);
}
But I don't want to force users to enter password. Is there any other solution using SMTP?
The error I am getting is A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
I have this function in my utility class , and it works like a charm.
public static bool SendMail(string Email, string MailSubject, string MailBody)
{
bool isSent = false, isMailVIASSL = Convert.ToBoolean(ConfigurationManager.AppSettings["MailServerUseSsl"]);
string mailHost = ConfigurationManager.AppSettings["MailServerAddress"].ToString(),
senderAddress = ConfigurationManager.AppSettings["MailServerSenderUserName"].ToString(),
senderPassword = ConfigurationManager.AppSettings["MailServerSenderPassword"].ToString();
int serverPort = Convert.ToInt32(ConfigurationManager.AppSettings["MailServerPort"]);
MailMessage msgEmail = new MailMessage(new MailAddress(senderAddress), new MailAddress(Email));
using (msgEmail)
{
msgEmail.IsBodyHtml = true;
msgEmail.BodyEncoding = System.Text.Encoding.UTF8;
msgEmail.Subject = MailSubject;
msgEmail.Body = MailBody;
using (SmtpClient smtp = new SmtpClient(mailHost))
{
smtp.UseDefaultCredentials = false;
smtp.EnableSsl = isMailVIASSL;
smtp.Credentials = new NetworkCredential(senderAddress, senderPassword);
smtp.Port = serverPort;
try
{
smtp.Send(msgEmail);
isSent = true;
}
catch (Exception ex)
{
throw ex;
}
}
}
return isSent;
}
<!-- Mail Server Settings -->
<add key="MailServerAddress" value="smtp.gmail.com" />
<add key="MailServerPort" value="25" />
<add key="MailServerSenderUserName" value="username#gmail.com" />
<add key="MailServerSenderPassword" value="password" />
<add key="MailServerUseSsl" value="True" />
I am trying to send a mail, in this case a gridview to a specified folder on my machine as to be able to view the message. I am thus sending the mail but it is not ending up in the folder. How can I do this?
I added this to web.config:
<system.net>
<mailSettings >
<smtp deliveryMethod="Network" from="ArianG#lr.co.za">
<network host="staging.itmaniax.co.za"/>
<specifiedPickupDirectory pickupDirectoryLocation="C:\testdump\emaildump\"/>
</smtp>
</mailSettings>
This is my code for sending the gridview. (I presume I do not need SmtpClient as I do not want to connect to a port, either 25 or 587) :
private void MailReport()
{
//*****************************************************
string to = "arianul#gmail.com";
string From = "ArianG#lr.co.za";
string subject = "Report";
string Body = "Good morning, Please view attachment<br> Plz Check d Attachment <br><br>";
Body += GridViewToHtml(GridView1);
Body += "<br><br>Regards,<br>Arian Geryts(ITManiax)";
bool send = sendMail(to, From, subject, Body);
if (send == true)
{
string CloseWindow = "alert('Mail Sent Successfully!');";
ClientScript.RegisterStartupScript(this.GetType(), "CloseWindow", CloseWindow, true);
}
else
{
string CloseWindow = "alert('Problem in Sending mail...try later!');";
ClientScript.RegisterStartupScript(this.GetType(), "CloseWindow", CloseWindow, true);
}
//*****************************************************
}
public bool sendMail(string to, string from, string subject, string body)
{
bool k = false;
try
{
MailMessage msg = new MailMessage(from, to);
msg.Subject = subject;
AlternateView view;
SmtpClient client;
StringBuilder msgText = new StringBuilder();
view = AlternateView.CreateAlternateViewFromString(msgText.ToString(), null, "text/html");
msg.AlternateViews.Add(view);
msgText.Append(" <html><body><br></body></html> <br><br><br> " + body);
//*****
/*client = new SmtpClient("smtp.gmail.com", 25);
client.Host = "staging.itmaniax.co.za";
client.Port = 25;
//****
client.EnableSsl = true;
client.Send(msg);*/
k = true;
}
Change your mail Setting in web.config to:
<smtp deliveryMethod="SpecifiedPickupDirectory">
<specifiedPickupDirectory pickupDirectoryLocation="C:\smtp" />
This should do the trick. Alternativly you could change the setting via tha IIS gui, after you deployed the solution.
Kind regards.
/edit: of course you need a smtp client. The program has to fire the email message out to the smtp server. The message just gets picked up by IIS and stuffed into a folder.