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
Related
Introduction
I currently have a C# method that allows me to send emails (see below). The emails are being sent via a Microsoft Exchange server, and the emails are read using an Outlook client. I would like to add additional functionality to this method so that I can include a follow-up reminder for 24 hours after the send time. From what I have read online, I need to use Microsoft.Office.Interop.Outlook, but I haven't been able to figure out how to use it.
Current method
using System.Net.Mail;
public void SendEmail(string host, string un, string pw, string from, string to, string subject, string body)
{
MailMessage email = new MailMessage(from, to);
email.Subject = subject;
email.Body = body;
SmtpClient client = new SmtpClient();
client.UseDefaultCredentials = false;
client.EnableSsl = true;
client.Credentials = new NetworkCredential(un, pw);
client.Host = host;
client.Send(email);
}
What I Have Tried
I believe I need to use the Microsoft.Office.Interop.Outlook.MailItem class to create the email instead of the MailMessage class I have been using. However I am unsure about 2 things:
How to connect to the server and send the email. I tried using the SmtpClient class as I was before, but I can't pass it a MailItem.
I'm not sure if my code to create the reminder is correct. I haven't been able to send the message, so I also haven't been able to see if the reminder is displayed in Outlook correctly. If anyone has experience using reminders, comments on how I am creating the reminder would also be appreciated.
Here is the code I have used to create the email and reminder:
using Microsoft.Office.Interop.Outlook;
public void SendEmail(string host, string un, string pw, string from, string to, string subject, string body)
{
MailItem email = new MailItem();
email.ReminderTime = new DateTime();
email.ReminderTime = DateTime.Now;
email.ReminderTime.AddDays(1);
email.ReminderSet = true;
email.Subject = subject;
email.Body = body;
}
I am trying to send out an email to a mailing list that I have created, but when I run the function it just times out. I am not sure whether it is a host error or if I'm missing code or what. I am running an mvc format program on a local server.
I originally didn't have the credentials, but adding them changed nothing. Both before and after adding them all that happens is it loads for about a minute before reporting a timeout.
private void SendEmail(string sender, string[] attachments, List<string> recipients, ReleaseNotes notes, string username, string password)
{
SmtpClient client = new SmtpClient();
MailMessage mail = new MailMessage();
mail.Subject = "Software Release of VCM Version " + notes.ReleaseVersion;
mail.From = new MailAddress(sender);
mail.Body = GetEmailBody(notes);
mail.IsBodyHtml = true;
foreach (string r in recipients)
{
mail.To.Add(r);
}
foreach (string a in attachments)
{
mail.Attachments.Add(new Attachment(a));
}
client.Host = "pod51213.outlook.com";
client.Credentials = new NetworkCredential(username, password);
client.UseDefaultCredentials = false;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Send(mail);
}
I am supposed to see the email appear in my inbox while the screen switches over to a screen saying email was sent. Its not giving me an actual error code. It just says operation timed out.
I was able to get the host I needed and get the function to run. The company I wrote the code for had their own host server. All I did to fix the code was delete the line that included client.Credentials, because I did not need that, and in the quotes for client.Host I replaced "pod51213.outlook.com" with "smtp.irco.com", which is the mail server for the company I built the program for.
I Think you should specify the port number
e.g.
SmtpClient("smtp.gmail.com", 587) // This for gmail
First of all, my apologies if this is a duplicate question. I have searched for it a lot, but couldn't find related issues.
So here's the problem: I am using SmtpClient and MailMessage class to send mails. I am passing the subject of the mail as a parameter in the mail sending method. First time the mail is sent with the proper subject (the one i sent as parameter). However, in all next emails, no matter what subject i put, the subject remains the same (the one used first time). The subject is set from inside of the method.
(Note: This is a WindowsForm application)
What i have tried is, creating another method named "Refresh()" which disposes the mail object and creates it again (with from and to info only). And call this method each time after a mail is sent. But it doesn't help with this problem.
Codes are given below:
Fields:
MailMessage message;
SmtpClient mailer;
string from = "sender email";
string pass = "sender pass";
string to = "rec email";
Constructor:
try
{
message = new MailMessage(from, to);
mailer = new SmtpClient("smtp.gmail.com", 587);
mailer.Credentials = new NetworkCredential(from, pass);
mailer.EnableSsl = true;
}
catch(Exception ex) { /*code to write log*/ }
Refresh method:
void RefreshMessage()
{
try
{
message.Subject = "";
message.Dispose();
message = new MailMessage(from, to);
}
catch(Exception ex) { /*write log*/ }
}
Method which is sending the mail:
internal void TextOnly(string sub, string bodyMessage)
{
try
{
message.Subject = sub;
message.Body = bodyMessage;
mailer.Send(message);
this.RefreshMessage();
}
catch (Exception ex) { /*write log*/ }
}
Example of how it's called:
m.TextOnly("Subject 1" , SomeStringMethod());
m.TextOnly("Another Title " + anyString, "Some string mail");
m.TextOnly("[TAG] Email subject goes here" , AnotherStringMethod());
Now no matter whatever subject is sent in the parameter, it will always send with subject "Subject 1" (from the example above). The body of the message is fine, only the subject is not right.
I have few other methods in the class (for other purposes like sending mails with attachments for example), where subject isn't passed as parameter but it's set directly from within the method (like message.Subject = "Example Sub" from within the method), in that case it works fine.
But in the case above, where the subject is passed to the method, the subject remains the same.
Like the comment section already stated, there is no reason to cache the message itself. Currently, you're disposing the message(which actually puts it in a unusable state) and then you recreate it. Check out more HERE. You can just as well simply create new objects and dispose of them after you're done so the Garbage Collector can release the resources as soon as possible.
Just utilize a simple method for constructing MailMessages and send them directly.
internal MailMessage ConstructTextMailMessage(MailAddress from, MailAddress to, string subject, string body)
{
return ConstructTextMailMessage(from.Address, to.Address, subject, body);
}
internal MailMessage ConstructTextMailMessage(string from, string to, string subject, string body)
{
return new MailMessage(from, to, subject, body);
}
And then:
var mailClient = new SmtpClient("smtp.gmail.com", 587);
mailClient.Credentials = new NetworkCredential(from, pass);
mailClient.EnableSsl = true;
mailClient.Send(ConstructTextMailMessage(from, to, "Subject 1", SomeStringMethod()));
mailClient.Send(ConstructTextMailMessage(from, to, "Another Title " + anyString, "Some string mail");
mailClient.Send(ConstructTextMailMessage(from, to, "[TAG] Email subject goes here", AnotherStringMethod());
If you have attachments in the MailMessage, you should call Dispose after using them to clear up the streams. Also, call Dispose on the SmtpClient when you're done using it.
I used the same functionality (SntpClient, MailMessage etc.) in one of my programms and it worked just fine:
SmtpClient client = new SmtpClient("host", port);
MailMessage mail;
MailAddress absender = new MailAddress("mail#adress.from");
foreach (string sub in Subjects)
{
mail = new MailMessage();
mail.IsBodyHtml = true;
mail.Subject = sub;
mail.From = absender;
mail.To.Add("mail#adress.to");
client.Send(mail);
}
Mybe you just need to make a new MailMessage-Object each time you "create" a E-Mail.
Is there any way to configure forward-to email address like replyto in System.Net.Mail.MailMessage?
If not then is there any way I can achieve that?
No, there is not.
The reason is that there is no such field defined in an email. You can see the defined fields here.
So this is not a matter of C# API, there is no way to do what you want to do, not in C# and not in other languages/frameworks.
You can do this only by doing it via Outlook (using interop)
var newItem = mailItem.Forward();
newItem.Recipients.Add("test#test.be");
newItem.Send();
the .Forward() is described here
There's no 'forwardto' in MailMessage (that's up to the user receiving the mail).
But you can send to multiple receipients at once, using either the CC or the BCC properties.
Here's an axample using CC:
public static void CreateCopyMessage(string server)
{
MailAddress from = new MailAddress("ben#contoso.com", "Ben Miller");
MailAddress to = new MailAddress("jane#contoso.com", "Jane Clayton");
MailMessage message = new MailMessage(from, to);
// message.Subject = "Using the SmtpClient class.";
message.Subject = "Using the SmtpClient class.";
message.Body = #"Using this feature, you can send an email message from an application very easily.";
// Add a carbon copy recipient.
MailAddress copy = new MailAddress("Notification_List#contoso.com");
message.CC.Add(copy);
SmtpClient client = new SmtpClient(server);
// Include credentials if the server requires them.
client.Credentials = CredentialCache.DefaultNetworkCredentials;
Console.WriteLine("Sending an email message to {0} by using the SMTP host {1}.",
to.Address, client.Host);
try {
client.Send(message);
}
catch (Exception ex) {
Console.WriteLine("Exception caught in CreateCopyMessage(): {0}",
ex.ToString() );
}
}
Now you can send to more than one, which Works like auto forwarding.
I am working with sending email through C# in asp.net, with IIS7.
But I couldn't fix the error.
I cannot even send a message to this path
C:\inetpub\mailroot\Queue
This is the simplest code I have used
MailMessage m = new MailMessage();
m.From = TextBox1.Text;
m.To = TextBox2.Text;
m.Subject = TextBox3.Text;
m.Body = TextBox4.Text;
SmtpMail.SmtpServer = "localhost"; //your real server goes here
SmtpMail.Send(m);
note: that I manually created this path, isn't wrong? or should be created automatically?
Please HELP!
If you manually created the path, then the SMTP Service is not installed on your machine (assuming that you intend to use this and not a third-party SMTP server).
You can verify this by looking in the list of services (I believe it starts with Simple Mail Transport...).
Perhaps it's the SmtpCredentials that's missing? And as the earlier speekers have said that the SmtpServer is it really localhost, meaning that you have a smtpserver on your machine?
I've done a SMTP mail to send som emails from my program that is working, perhaps you can find something in this that can help you?
It looks like this.
public class Email
{
public Email(string recieverAdress)
{
mail = new MailMessage(senderAdress, recieverAdress);
}
private readonly MailMessage mail;
private readonly SmtpClient smtpClient = new SmtpClient("smtp.domain.com", port);
private readonly NetworkCredential credential = new NetworkCredential("username", "password");
public void SendMail(string subject, string textInBody)
{
mail.Subject = DateTime.Now + " " + subject;
mail.Body = textInBody;
smtpClient.Credentials = credential;
smtpClient.Send(mail);
}
}