I am creating my mvc app. I would like to send email to users that register. I do it like this:
MailMessage mail = new MailMessage();
mail.To.Add(user1.email.Replace(" ", string.Empty));
mail.From = new MailAddress("declarations#virtual.mini.pw.edu.pl");
mail.Subject = "Class Declaration System user creation confirmation.";
string body = "Dear " + user1.name + ",\n This is to confirm that you have succesfully registered in the system. \n Do not reply to this message. \n Regards";
mail.Body = body;
mail.IsBodyHtml = true;
var smtp = new SmtpClient();
smtp.EnableSsl = true;
smtp.Host = "poczta.mini.pw.edu.pl";
smtp.Port = 25;
And everything seems fine. I have contacted the administrator of the server and I am sending everything through a right port. However, I get an error:
Command parameter not implemented. The server response was: 5.5.2
> <WIN-KI3H68FIO4E>: Helo command rejected: need fully-qualified hostname
The administrator told me that this is because I am trying to send it as WIN-KI3H68FIO4E, but it should be something.mini.pw.edu.pl, since we have such restrictions here. How do I change it in my code?
The answer was to edit the web.config file and to add there custom ClientDomain.
Related
I am using MVC framework 4.5 C# and publish the my project on Windows server 2012 R2. In that server when I tried to sending mail with gmail but its can not send the mail and giving the below description.
The SMTP server requires a secure connection or the client was not
authenticated.The server response was: 5.5.1 Authentication Required.
I installed SMTP Service and all configuration regarding that.
Same mail configuration is running my development server.
It is due to security check on gmail so please follow below steps.
Log in to production server via remote access, and sign in to gmail once with your credentials. They will ask for the confirmation, confirm it
SmtpClient client = new SmtpClient("smtp.gmail.com");
client.Credentials = new System.Net.NetworkCredential("your email address", "password");
client.Port = 587;
client.EnableSsl = true;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
// Specify the email sender.
// Create a mailing address that includes a UTF8 character
// in the display name.
MailAddress from = new MailAddress("your email address");
// Set destinations for the email message.
MailAddress to = new MailAddress(textBox_SedToEmail.Text);
// Specify the message content.
MailMessage message = new MailMessage(from, to);
message.Body = "This is a test email message sent by an application. ";
// Include some non-ASCII characters in body and subject.
string someArrows = new string(new char[] { '\u2190', '\u2191', '\u2192', '\u2193' });
message.Body += Environment.NewLine + someArrows;
message.BodyEncoding = System.Text.Encoding.UTF8;
message.Subject = "test message 1" + someArrows;
message.SubjectEncoding = System.Text.Encoding.UTF8;
message.Attachments.Add(new Attachment(#"C:\Users\eddie\Pictures\2.jpg"));
// Set the method that is called back when the send operation ends.
client.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);
// The userState can be any object that allows your callback
// method to identify this send operation.
// For this example, the userToken is a string constant.
string userState = "test message1";
client.SendAsync(message, userState);
I have tried to send email to my client's customers with my client from address(info#myclient.com).
and my client has already configured my smtp server via SPF to their domain.
Following is C# code written to send the email.
MailMessage mail = new MailMessage();
//set the addresses
mail.From = new MailAddress("info#mycient.com");
mail.To.Add("krishna.menan#gmail.com");
mail.Subject = "This is an email";
mail.Body = "this is a sample body with html in it. <b>This is bold</b>
<font color=#336699>This is blue</font>";
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
System.Net.NetworkCredential networkCredentialRFE = new
System.Net.NetworkCredential("kirshna#mycomapny.com", "Password123");
smtp.Credentials = networkCredentialRFE;
smtp.Host = "outlook.office365.com";
smtp.EnableSsl = true;
smtp.Port = 587;
smtp.Send(mail);
Following is error I am getting:
Mailbox unavailable. The server response was: 5.7.60 SMTP; Client does not have permissions to send as this sender
Make sure that the taskuser you are using has the proper "send as" permissions, see "Give mailbox permissions to another user in Office 365 - Admin Help".
I am developing a program which needs to have the capability to send emails. I've got a simple mail function setup however I have never really delved into the mail side of things and am not sure if I'm using the correct settings.
I am sure I am doing something wrong with the SMTP, I have set the MailMessage host as the outgoing mail server that I use for emailing from outlook (the email accounts are hosted on shared virtual hosting so I use their supplied hostname in the function) alongside the login credentials I would normally use. When I try to send a test email it throws an unable to connect to remote server exception. I have WAMPSERVER setup on the computer I am trying to run this from, I know it has some kind of SMTP capability? Should I be using this or is there no reason I can't use shared virtual hosting SMTP as the host? Please refer to code below-
public void EmailTracking()
{
string to = "johnsmith#xxxxxxxxxxxxx.com.au";
string body = "this is some text for body";
string subject = "subject line";
string fromAddress = "kelvie#xxxxxxxxxx.com.au";
string fromDisplay = "Kelvie";
string credentialUser = "removed";
string credentialPassword = "removed";
string host = "removed";
MailMessage mail = new MailMessage();
mail.Body = body;
mail.IsBodyHtml = true;
mail.To.Add(new MailAddress(to));
mail.From = new MailAddress(fromAddress, fromDisplay, Encoding.UTF8);
mail.Subject = subject;
mail.SubjectEncoding = Encoding.UTF8;
mail.Priority = MailPriority.Normal;
SmtpClient smtp = new SmtpClient();
smtp.Credentials = new System.Net.NetworkCredential(credentialUser, credentialPassword);
smtp.Host = host;
smtp.Send(mail); //fails here with unable to connect to remote server
}
I am stuck and not able to receive email from yahoo id(if the sender email id is of yahoo).
code is working fine not giving me any error and i am receiving email fromm gmail id.
i am using localhost (not in local machine on live server).
hosting server : smtp.snapgrid.com
also used authentication,enable ssl , using proper port for ssl.
on snapgrid i do check so i got is mail from yahoo is blocked and the message is,
message :
Type: blocked
Reason: 550 5.7.1 Unauthenticated email from yahoo.com is not accepted due to domain's
DMARC policy. Please contact administrator of yahoo.com domain if this was a legitimate
mail. Please visit http://support.google.com/mail/answer/2451690 to learn about DMARC
initiative.
please help...
code i used to send is(its working fine just giving for idea):
Method 1:
SmtpClient objSMTPClient = new SmtpClient();
objSMTPClient.Host = ConfigurationManager.AppSettings["strSMTPServer"];
string BODY_FORMAT = ConfigurationManager.AppSettings["EmailBodyContentFormat"];
MailMessage objMailMessage = new MailMessage(from.Trim(), to.Trim(), subject.Trim(), body.Trim());
objSMTPClient.UseDefaultCredentials = false;
if (BODY_FORMAT.ToUpper() == "HTML")
objMailMessage.IsBodyHtml = true;
else if (BODY_FORMAT.ToUpper() == "TEXT")
{
body = StripTags(body);
objMailMessage.IsBodyHtml = false;
objMailMessage.Body = body.ToString().Trim();
}
else
return false;
objSMTPClient.Send(objMailMessage);
return true;
Method 2:
SmtpClient oMail = new SmtpClient();
MailMessage msg = new MailMessage();
MailAddress Madd = new MailAddress(from, "sunil");
oMail.Host = "smtp.gmail.com";
oMail.Port = 587;
oMail.EnableSsl = true;
oMail.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
oMail.Credentials = new NetworkCredential("sunil123#mydomain.com", "******");
oMail.Timeout = 20000;
msg.From = Madd;
msg.Body = body.ToString();
msg.To.Add(to);
msg.Subject = subject;
msg.IsBodyHtml = true;
oMail.Send(msg);
return true;
both are working having no bug running without error....
If you are sending via a server belonging to someone like Yahoo, Google or Office365 they expect the sender name of the account to match that that you're sending using in the from address.
For example, this would work on a your local SMTP server:
Message.From = new MailAddress("GrandMasterFlush#domain.com");
However, to get it to send via someone like Yahoo would require you to send it like this:
Message.From = new MailAddress("GrandMasterFlush#domain.com", "Grandmaster Flush");
If the sender name provided does not exactly match that of the account the email will not get sent.
I have the following code but I am getting an exception that a smtp host is not defined. If I am running this and testing on my local machine from visual studio, what do I need to do to be able to send email from my machine. Do I have to turn on some Windows service?
private void SendMailToAdminToApprove(string email_, string name_)
{
MailMessage msg = new MailMessage();
msg.From = new MailAddress("address#domain.com", "Person's Name");
msg.To.Add(new MailAddress("a#gmail.com", "Adam"));
msg.Subject = "Message Subject";
msg.Body = "Mail body content";
msg.IsBodyHtml = true;
msg.Priority = MailPriority.High;
try
{
SmtpClient c = new SmtpClient();
c.Send(msg);
}
catch (Exception ex)
{
Console.Write("T");
}
}
You need to set the SMTP host to point to an actual SMTP server. One option is to run the SMTP service on your own machine, but you could also point to your ISP's server.
edit
As pcampbell and Skeolan mentioned, the actual value should go into app.config. I'm not sure if localhost would be an exception: it would depend on whether you want the option of not running a local server.
You'll need to specify the SMTP host here:
string smtpHost = "localhost";
//or go to your config file
smtpHost = ConfigurationManager.AppSettings["MySmtpHost"].ToString();
SmtpClient c = new SmtpClient(smtpHost);
You need to define the SMTP relay:
SmtpClient c = new SmtpClient("relay.yourdomain.com");
or if you're running the relay locally:
SmtpClient c = new SmtpClient("localhost");
You should change this section:
SmtpClient c = new SmtpClient();
// Either specify a SMTP server above, or set c.Host
c.Send(msg);
You need to specify which SMTP server is going to be used for sending this message. If you install a SMTP server locally, this could be localhost - but otherwise, you'll need to have an outgoing mail server set appropriately.
Here is the code I use for sending email with C#. I also commented out code for sending it to a file locally if you need it.
SmtpClient smtp = new SmtpClient(smtpServer, portNumber);
// Disable SSL when saving to directory.
smtp.EnableSsl = true;
smtp.Credentials = new NetworkCredential(mailFrom, password);
// Set mail to be delivered to a folder
//smtp.PickupDirectoryLocation = #"C:\mail\Send";
//smtp.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;