I have an event for a button, that simply emails me whatever the user entered. I get the message fine....subject....message. But the from email part is just showing from myself (same as the one it is going to)
How to do I have it show from whatever email address they entered?
I'm using gmail smtp to a gmail account that I have set up.
MailMessage has 4 parameters (from, to, subject, body)
txtEmail.Text does hold their email address correctly.
protected void Wizard1_FinishButtonClick(object sender, WizardNavigationEventArgs e)
{
this.toEmail = "myemail#gmail.com";
this.subject = txtSubject.Text;
this.fromEmail = txtEmail.Text;
this.comment = txtComment.Text;
message = new MailMessage(fromEmail, toEmail, subject, comment);
smtp.Send(message);
message.Dispose();
}
I tried the suggestion like below with something like this... and still showing from myself.
message = new MailMessage(ReplyToList[0].toString(), toEmail, subject, comment);
I even tried doing it this way and still shows from myself. I even stepped through the code to make sure, it was holding different email addresses and it is.
protected void Wizard1_FinishButtonClick(object sender, WizardNavigationEventArgs e)
{
this.subject = txtSubject.Text;
this.comment = txtComment.Text;
to = new MailAddress("myemail#gmail.com");
from = new MailAddress(txtEmail.Text);
MailMessage message = new MailMessage(from, to);
message.Subject = txtSubject.Text;
message.Body = txtComment.Text;
message.Headers.Add("Reply-To", txtEmail.Text);
smtp.Send(message);
message.Dispose();
}
In the code I just call SmtpClient client = new SmtpClient();
Then in my web.config I have
<mailSettings>
<smtp from="bob">
<network host="smtp.gmail.com" port="587" userName="myemail" password="mypassword" enableSsl="true"/>
</smtp>
</mailSettings>
any help?
As James Manning has suggested an easy way of doing this would be to set a reply-to header on the email before sending as follows:
this.ReplyToList.Add(txtEmail.Text);
Try creating your SmtpClient like this (you didn't include it so this may not be your problem).
var client
= new SmtpClient(SmtpHostname)
{
DeliveryMethod = SmtpDeliveryMethod.Network,
Credentials = CredentialCache.DefaultNetworkCredentials
};
This always has worked for me although not verified through gmail smtp servers:
MailMessage message = new MailMessage(from, to);
message.ReplyToList.Add(new MailAddress(from));
message.Subject = txtSubject.Text;
Related
I build a web form on my website to allow visitors to send me a message. Something very basic (for my personal use). On my backend (c# .Net MVC) I use SmtpClient to send the mail. I use my hotmail account for that purpose. It works. Please note that from is equal to the username used in Credentials.
SmtpClient _client = new SmtpClient();
_client.Host = "smtp.live.com";
_client.Port = 587;
_client.UseDefaultCredentials = true;
_client.Credentials = new System.Net.NetworkCredential("ttttt#hotmail.com", "mypassword");
_client.EnableSsl = true;
_client.DeliveryMethod = SmtpDeliveryMethod.Network;
MailAddress to = new MailAddress("ttttt#gmail.com");
MailAddress from = new MailAddress("ttttt#hotmail.com");
MailMessage mail = new MailMessage(from, to);
mail.Subject = "The subject";
mail.Body = "The message";
_client.Send(mail);
When I receive the mail, the sender is equal to the receiver ( myself :) This is not ideal.
From: John Doe
To: John Doe
Subject: My subject
Message: My message
I would prefer having the visitor's email address in the sender (the from). So I try to change that but it doesn't work. I got the error message below:
System.Net.Mail.SmtpException Transaction failed. The server response was: 5.2.0 STOREDRV.Submission.Exception:SendAsDeniedException.MapiExceptionSendAsDenied; Failed to process message due to a permanent exception with message Cannot submit message.
I understand that this is not working for security reason. I cannot send an email 'in the name of' someone else.
Before giving up, I come here in the hope of someone can suggest me an alternative.
PS: I know I can use mail.ReplyToList.Add(emailofvisitor); then when I press Reply this is the visitor's email which is used but this is not still ideal because I still see my ttttt#hotmail.com in the from field.
Hope I didn't misunderstand your request, I'm assuming you would like to change the displayed name for the sender, this is what I do in my project, Add the following to your Web.Config or App.Config file:
Fill the from part with whatever you want and it will show it as the sender name.
<system.net>
<mailSettings>
<smtp from="YourDisplayName <YourEmail>" deliveryMethod="Network">
<network defaultCredentials="false" enableSsl="true" host="hostname" port="587" userName="yourusername" password="yourpassword"/>
</smtp>
</mailSettings>
</system.net>
C# code:
SmtpClient smtpClient = new SmtpClient();
var smtpSection = (SmtpSection)ConfigurationManager.GetSection("system.net/mailSettings/smtp");
MailMessage message = new MailMessage();
message.From = new MailAddress(smtpSection.From);
MailAddress to = new MailAddress("youremail#gmail.com");
message.To.Add(to);
message.Subject = "The subject";
message.Body = "The message";
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.Send(message);
I have a web application using ASP.net and C#,in one step it will need
from the user to
send an email to someone with an attachments.
my problem is when the user will send the email i don't want to put their
password every time the user send.
i want to send an email without the password of the sender.
any way to do that using SMTP ?
and this is a sample of my code "not all".
the code is worked correctly when i put my password , but without it ,it
is not work, i need a way to send emails without put the password but
in the same time using smtp protocol.
private void button1_Click(object sender, EventArgs e)
{
string smtpAddress = "smtp.office365.com";
int portNumber = 587;
bool enableSSL = true;
string emailFrom = "my email";
string password = "******";
string emailTo = "receiver mail";
string subject = "Hello";
string body = "Hello, I'm just writing this to say Hi!";
using (MailMessage mail = new MailMessage())
{
mail.From = new MailAddress(emailFrom);
mail.To.Add(emailTo);
mail.Subject = subject;
mail.Body = body;
mail.IsBodyHtml = true;
// Can set to false, if you are sending pure text.
// mail.Attachments.Add(new Attachment("C:\\SomeFile.txt"));
// mail.Attachments.Add(new Attachment("C:\\SomeZip.zip"));
using (SmtpClient smtp = new SmtpClient(smtpAddress,portNumber))
{
smtp.UseDefaultCredentials = true;
smtp.Credentials = new NetworkCredential(emailFrom, password);
smtp.EnableSsl = enableSSL;
smtp.Send(mail);
}
MessageBox.Show("message sent");
}
}
I believe this can be accomplished easily, but with some restrictions.
Have a look at the MSDN article on configuring SMTP in your config file.
If your SMTP server allows it, your email object's from address may not need to be the same as the credentials used to connect to the SMTP server.
So, set the from address of your email object as you already are:
mail.From = new MailAddress(emailFrom);
But, configure your smtp connection one of two ways:
Set your app to run under an account that has permission to access the SMTP server
Include credentials for the SMTP server in your config, like this.
Then, just do something like this:
using (SmtpClient smtp = new SmtpClient())
{
smtp.Send(mail);
}
Let the configuration file handle setting up SMTP for you. This is also great because you don't need to change any of your code if you switch servers.
Just remember to be careful with any sensitive settings in your config file! (AKA, don't check them into a public github repo)
First of all, I search for an hour how to solve my problem on other posts but the other solutions don't work in my case.
My problem
I need to send a report mail after the execution of my program. To send mails I use System.Net.Mail namespace and particularly SmtpClient class.
The mail is correctly sent but I need to hide the sender mail address.
I tried some different things but none of them seems to work.
What I tried
Firstly I tried to do this :
public static void sendMail(String Titre,String Message)
{
SmtpClient client = new SmtpClient(GestionParametres.getParametre("SMTP"), Int32.Parse(GestionParametres.getParametre("PortSmtp")));
client.Credentials = new System.Net.NetworkCredential(GestionParametres.getParametre("UsernameSmtp"), GestionParametres.getParametre("PasswordSmtp"));
MailAddress from = new MailAddress(GestionParametres.getParametre("ExpediteurMail"),"Rapport interface ****");
MailAddress to = new MailAddress(GestionParametres.getParametre("DestinataireMail"));
MailMessage message = new MailMessage(GestionParametres.getParametre("ExpediteurMail"), GestionParametres.getParametre("DestinataireMail"));
message.From = from;
message.Subject = Titre;
message.Body = Message;
message.BodyEncoding = System.Text.Encoding.UTF8;
client.Send(message);
}
But the sender mail address still appear in mail.
Secondly, I tried this :
public static void sendMail(String Titre,String Message)
{
SmtpClient client = new SmtpClient(GestionParametres.getParametre("SMTP"), Int32.Parse(GestionParametres.getParametre("PortSmtp")));
client.Credentials = new System.Net.NetworkCredential(GestionParametres.getParametre("UsernameSmtp"), GestionParametres.getParametre("PasswordSmtp"));
MailAddress from = new MailAddress(GestionParametres.getParametre("ExpediteurMail"));
MailAddress to = new MailAddress(GestionParametres.getParametre("DestinataireMail"));
MailMessage message = new MailMessage("Rapport interface ****" + GestionParametres.getParametre("ExpediteurMail"), GestionParametres.getParametre("DestinataireMail"), Titre, Message);
client.Send(message);
}
But it doesn't work either...
Now I have no idea how to solve this problem.
Any idea ?
Thank you in advance,
Thomas
i am using using System.Net.Mail;
and following code to send mail
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;
// Set the SMTP server to be used to send the message
client.Host = "YourMailServer";
// Send the e-mail message
client.Send(message);
for Host i am providing client.Host = "localhost";
for this its falling with error
No connection could be made because the target machine actively
refused it some_ip_address_here
and when i use client.Host = "smtp.gmail.com";
i get following error
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 am not able to send mail through localhost.
Please help me out, i am new to c# please correct me in code where i am going wrong..?
Here is some code that works for sending mail via gmail (code from somewhere here on stackoverflow. It is similar to the code here: Gmail: How to send an email programmatically):
using (var client = new SmtpClient("smtp.gmail.com", 587)
{
Credentials = new NetworkCredential("yourmail#gmail.com", "yourpassword"),
EnableSsl = true
})
{
client.Send("frommail#gmail.com", "tomail#gmail.com", "subject", message);
}
For sending mail from client.Host = "localhost" you need set up local SMTP server.
For sending mail via Google (or via any other SMTP server, including your own local SMTP) you must set username, password, ssl settings - all as required by SMTP server chosen, and you need to read their help for this.
For example Google says that you need SSL, port 465 or 587, server smtp.gmail.com and your username and password.
You can assign all this values in .config file.
<system.net>
<mailSettings>
<smtp>
<network host="smtp.gmail.com" enableSsl="true" port="587" userName="yourname#gmail.com" password="password" />
</smtp>
</mailSettings>
</system.net>
Or set to SmtpClient in code before every use:
client.Host = "smtp.gmail.com";
client.Port = 587;
client.EnableSSL = true;
client.Credentials = new NetworkCredential("yourname#gmail.com", "password");
Place this code inside a <configuration> </configuration> in web.config file
<system.net>
<mailSettings>
<smtp>
<network host="smtp.gmail.com" enableSsl="true" port="587" userName="youremail#gmail.com" password="yourpassword" />
</smtp>
</mailSettings>
</system.net>
then backend code
MailMessage message = new MailMessage();
message.IsBodyHtml = true;
message.From = new MailAddress("email#gmail.com");
message.To.Add(new MailAddress(TextBoxEadd.Text));
message.CC.Add(new MailAddress("email#gmail.com"));
message.Subject = "New User Registration ! ";
message.Body = "HELLO";
sr.Close();
SmtpClient client = new SmtpClient();
client.Send(message);
I hope this code help you! :)
use this Line..Dont Use Port And HostName
LocalClient.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
I just wanted to add that Gmail now requires App Password in order to use it from other applications. Check this link. I had to find it the hard way. After I created an App Password and then I changed the NetworkCredentials to use it to send emails.
I am using the code as described in this question. However get following error when send an email.
Mailbox unavailable. The server response was: please authenticate to
use this mail server
Any ideas what could be wrong?
UPATE: Here is the code
System.Net.Mail.SmtpClient Client = new System.Net.Mail.SmtpClient();
MailMessage Message = new MailMessage("From", "To", "Subject", "Body");
Client.Send(Message);
With following in App.config.
<system.net>
<mailSettings>
<smtp from="support#MyDomain1.com">
<network host="smtp.MyDomain1.com" port="111" userName="abc" password="helloPassword1" />
</smtp>
</mailSettings>
</system.net>
The code posted there should work. if it doesn't, you might try setting the username and password in code-behind rather than reading them from the web.config.
Code sample from systemnetmail.com:
static void Authenticate()
{
//create the mail message
MailMessage mail = new MailMessage();
//set the addresses
mail.From = new MailAddress("me#mycompany.com");
mail.To.Add("you#yourcompany.com");
//set the content
mail.Subject = "This is an email";
mail.Body = "this is the body content of the email.";
//send the message
SmtpClient smtp = new SmtpClient("127.0.0.1");
//to authenticate we set the username and password properites on the SmtpClient
smtp.Credentials = new NetworkCredential("username", "secret");
smtp.Send(mail);
}
Yes, the smtp server is telling you that in order to relay email for you, you need to authenticate before attempting to send the email. If you have an account with the smptp server, you can set the credentials on the SmtpClient object accordingly. Depending on the authentication mechanism supported by the smtp server, the port, etc, will be different.
Example from MSDN:
public static void CreateTestMessage1(string server, int port)
{
string to = "jane#contoso.com";
string from = "ben#contoso.com";
string subject = "Using the new SMTP client.";
string body = #"Using this new feature, you can send an e-mail message from an application very easily.";
MailMessage message = new MailMessage(from, to, subject, body);
SmtpClient client = new SmtpClient(server, port);
// Credentials are necessary if the server requires the client
// to authenticate before it will send e-mail on the client's behalf.
client.Credentials = CredentialCache.DefaultNetworkCredentials;
try {
client.Send(message);
}
catch (Exception ex) {
Console.WriteLine("Exception caught in CreateTestMessage1(): {0}",
ex.ToString() );
}
}
The bottom line is that your credentials are not being passed to the Smtp server or else you wouldn't be getting that error.