Unable to send email through local SMTP server - c#

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);
}
}

Related

Sending emails with outlook reminder

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;
}

SmtpClient Send(Mail) function timeout error

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

send an email using SMTP without password in C#

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)

Change sender name doesn't work

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

Writing C# code to send an email with office 365

This seems like it should be simple to me, but I cannot get it to work. I can easily send emails with code like this:
[OperationBehavior(Impersonation = ImpersonationOption.Required)]
public static void sendEmail(string toEmail, string toName, string subject, string body)
{
MailAddress from = new MailAddress("TizzyFoe#MyCompany.com", "Mr Tizzy Foe");
const string UserName = "TizzyFoe#MyCompanyInc.com";
const string password = "ThisIsMyRealPassword";
const string host = "smtp.office365.com";
MailMessage msg = new MailMessage();
msg.To.Add(new MailAddress(toEmail, toName));
msg.From = from;
msg.Subject = subject;
msg.Body = body;
msg.IsBodyHtml = true;
SmtpClient client = new SmtpClient();
client.UseDefaultCredentials = true;
//client.Credentials = (System.Net.ICredentialsByHost)System.Net.CredentialCache.DefaultCredentials;
//client.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
client.Credentials = new System.Net.NetworkCredential(UserName, password);
client.Port = 587;
client.Host = host;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = true;
client.Send(msg);
}
But putting my password into code like that is bad for a variety of obvious reasons.
So what I want to do is say, okay this program is running on my laptop and I am logged in as me. So shouldn't the program be able to access my credentials. And you can see i commented out two failed attempts to do just that:
//client.Credentials = (System.Net.ICredentialsByHost)System.Net.CredentialCache.DefaultCredentials;
//client.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
both of those result in this exception:
The SMTP server requires a secure connection or the client was not
authenticated. The server response was: 5.7.57 SMTP; Client was not
authenticated to send anonymous mail during MAIL FROM
Maybe my program can only access some encrypted version of my credentials which cannot be passed to the office365 server? I have kind of a working knowledge of security concepts, but this is getting over my head.
I feel like this can't possible be that difficult. I know there are applications which can send automated emails through office 365. Right now, I'm just testing my code as a console app, but my thinking was ultimately I'd create a windows service to run it. Maybe the windows service would be able to pass credentials?

Categories

Resources