I'm using smtp (c#)and trying to send mail to group id(official id) but its not able to send mail.Although through same code I'm able to send mail to individual id. Any idea what could be wrong here.
Following code I'm using
MailMessage mail = new MailMessage();
mail.To = "group#company.com";
mail.From = "me#company.com";
mail.Subject = "Test Mail: please Ignore";
mail.Body = body;
SmtpMail.SmtpServer = "mailhub.int.company.com";
SmtpMail.Send(mail)
I'm getting following error in my mail box:
Delivery has failed to these recipients or distribution lists:
group#company.com
Not Authorized. You are not authorized to send to this recipient or distribution list. For distribution lists please check approved senders in the corporate directory.
_____
Sent by Microsoft Exchange Server 2007
Diagnostic information for administrators:
Generating server: GSPWMS005.int.company.com
group#company.com
#550 5.7.1 RESOLVER.RST.AuthRequired; authentication required ##
From error I could get an idea that some authentication is missing but not sure which one or how to resolve it.
If I send mail to this group thorough my outlook then its working ok.
http://msdn.microsoft.com/en-us/library/59x2s2s6.aspx
MailMessage message = new MailMessage(from, to);
message.Body = "This is a test e-mail message sent by an application. ";
message.Subject = "Test Email using Credentials";
NetworkCredential myCreds = new NetworkCredential("username", "password", "domain");
CredentialCache myCredentialCache = new CredentialCache();
try
{
myCredentialCache.Add("ContoscoMail", 35, "Basic", myCreds);
myCredentialCache.Add("ContoscoMail", 45, "NTLM", myCreds);
client.Credentials = myCredentialCache.GetCredential("ContosoMail", 45, "NTLM");
client.Send(message);
Console.WriteLine("Goodbye.");
}
catch(Exception e)
{
Console.WriteLine("Exception is raised. ");
Console.WriteLine("Message: {0} ",e.Message);
}
Related
I'm developing a third-party add-on to run in a program called M-Files.
The purpose of the add-on is to send a mail with the help of an SMTP server. I created a fake SMTP server in DevelMail.com just for testing.
Testing the SMTP server from a browser works but when i run the code it gives me the following error.
Transaction failed. The server response was: 5.7.1 Client host rejected: Access denied
Here are the SMTP information:
Host: smtp.develmail.com
SMTP Port: 25
TLS/SSL Port: 465
STARTTLS Port : 587
Auth types: LOGIN, CRAM-MD5
Here is the code:
MailAddress adressFrom = new MailAddress("notification#mfiles.no", "M-Files Notification Add-on");
MailAddress adressTo = new MailAddress("majdnakhleh#live.no");
MailMessage message = new MailMessage(adressFrom, adressTo);
message.Subject = "M-Files Add-on running";
string htmlString = #"<html>
<body>
<p> Dear customer</p>
<p> This is a notification sent to you by using a mailadress written in a metadata property!.</p>
<p> Sincerely,<br>- M-Files</br></p>
</body>
</html>
";
message.Body = htmlString;
SmtpClient client = new SmtpClient();
client.Host = "smtp.develmail.com";
client.Port = 587;
client.Credentials = new System.Net.NetworkCredential("myUserName", "myPassword");
client.EnableSsl = true;
client.Send(message);
Reason for the Issue:
Usually, email sending option using SMTP encountered Access denied
because there should have a sender email which required to allow
remote access. When SMTP request sent from the sender email
it checks whether there is remote access allowed. If no, then you
always got Access denied message.
Solution:
For example let's say, you want to send email using Gmail SMTP in that case you do have to enable Allow less secure apps: ON
How To Set
You can simply browse this link Less secure app access and turn that to ON
See the screen shot
Code Snippet:
public static object SendMail(string fromEmail, string toEmail, string mailSubject, string mailBody, string senderName, string senderPass, string attacmmentLocationPath)
{
try
{
MailMessage mail = new MailMessage();
//Must be change before using other than gmail smtp
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress(fromEmail);
mail.To.Add(toEmail);
mail.Subject = mailSubject;
mail.Body = mailBody;
mail.IsBodyHtml = true;
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential(senderName, senderPass);//Enter the credentails from you have configured earlier
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
return true;
}
catch (Exception ex)
{
return ex;
}
}
Note: Make sure, fromEmail and (senderName, senderPass) should be same email with the credential.
Hope that would help.
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 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.
SmtpClient smtpClient = new SmtpClient("smtp.gmail.com");
smtpClient.Credentials = new NetworkCredential("xxxxx#gmail.com", "password");
smtpClient.Port = 587;
smtpClient.EnableSsl = true;
MailMessage mail = new MailMessage();
//mail.From = new MailAddress("xxx1#gmail.com");
mail.To.Add("XX2#gmail.com");
mail.Subject = "Test mail";
mail.Body = "This is test mail, with test content";
smtpClient.Send(mail);
In the above code, why mail.From is mandatory? even I specified mail-id in smtpClient.Credentials.
Even I specified mail.From with some mail-id, the receiver is not receiving mail from this mail address, instead receiving from xxxxx#gmail.com which I specified in smtpClient.Credentials.
From is supposed to be the address from where the mail was sent.
This can be different from your Smtp credentials. Some servers will let you have a username that is different from your mail address.
Gmail shows the email address from where it received the mail.
They do that so that users don't mistake getting the mail from someone who didn't really send it.
Not all email clients do that.
You cannot fake a from address to gmail. They will always show where they got the email from.
I am trying to create a contact us page where visitor of the site can leave a message to site-admin.
say I m running this site
www.example.com
and i have an id contact#example.com
Now a new visitor visits my site and fills up the contact us form.
He fills
FROM - abc#yahoo.com
Subject- Query
Message- My question....
I want to send this message as a mail to contact#example.com
And on successfull sending of mail i want to send this user autogenerated mail as an acknowledgement.
What I need to do for this, My hosting server is GoDaddy
I am trying the following code.
try
{
MailMessage mailMessage = new MailMessage();
mailMessage.From = new MailAddress(txtEmailId.Text);
mailMessage.To.Add(new MailAddress("contact#example.com"));
mailMessage.Subject = txtSubject.Text;
mailMessage.Body = txtMessage.Text;
mailMessage.IsBodyHtml = false;
mailMessage.Priority = MailPriority.High;
SmtpClient sc = new SmtpClient("relay-hosting.secureserver.net");
//sc.Credentials = new System.Net.NetworkCredential("contact#example.com", "MyPassword");
sc.Send(mailMessage);
}
catch (Exception ex)
{
Label1.Text = "An Error has occured : "+ex.Message;
}
This error occurs while trying to send mail.
Mailbox name not allowed. The server
response was: sorry, your mail was
administratively denied. (#5.7.1)
It looks like from your code you are specifying that the mail message's From field is based on the email address the user fills in. The mail server mail be rejecting this and only allowing email from the domain name example.com or perhaps the server name itself. I've run into this before where your From field must be the original site domain...
If you want to have the ability for the form-mail recipient to directly reply, use the ReplyTo field rather than the from:
MailMessage mailMessage = new MailMessage();
//try using a domain address that matches your server and/or site.
//the email address itself may also have to exist depending on the mail server.
mailMessage.From = new MailAddress("no-reply#example.com");
//set the replyto field
mailMessage.ReplyTo = new MailAddress(txtEmailId.Text);
//send the mail...
Hope this might help...
EDIT:
After you've edited your question, the above is still relevant. It's very likely you cannot use a From address outside of your own site domain example.com. If you want to send two copies of the request (one to contact#example.com and the other to the user as an acknowledgment), you simply need to add to the To property:
MailMessage mailMessage = new MailMessage();
//try using a domain address that matches your server and/or site.
//the email address itself may also have to exist depending on the mail server.
mailMessage.From = new MailAddress("abc#example.com");
//add the contact email address to the To list
mailMessage.To.Add(new MailAddress("contact#example.com"));
//add the user email address to the To list
mailMessage.To.Add(new MailAddress(txtEmailId.Text));
//set the replyto field
mailMessage.ReplyTo = new MailAddress(txtEmailId.Text);
//send the mail...
If you need to add a simple message for the user acknowledgment email you could also do:
try
{
MailMessage mailMessage = new MailMessage();
mailMessage.From = new MailAddress("abc#example.com");
mailMessage.To.Add(new MailAddress("contact#example.com"));
mailMessage.ReplyTo = new MailAddress(txtEmailId.Text);
mailMessage.Subject = txtSubject.Text;
mailMessage.Body = txtMessage.Text;
mailMessage.IsBodyHtml = false;
mailMessage.Priority = MailPriority.High;
SmtpClient sc = new SmtpClient("relay-hosting.secureserver.net");
//sc.Credentials = new System.Net.NetworkCredential("contact#example.com", "MyPassword");
//send to only the contact#example.com first
sc.Send(mailMessage);
mailMessage.To.Clear();
mailMessage.To.Add(new MailAddress(txtEmailId.Text));
//add a simple message to the user at the beginning of the body
mailMessage.Body = "Thank you for submitting your question. The following has been submitted to contact#example.com: <br/><br/>" + mailMessage.Body;
//send the acknowledgment message to the user
sc.Send(mailMessage);
}
catch (Exception ex)
{
Label1.Text = "An Error has occured : "+ex.Message;
}
It is probably Serverfault.com question, any how
you need to check these options
Admin Home -> Configuration -> Email
Options -> - E-Mail Transport Method =
sendemail
reference 553 sorry, your mail was administratively denied. (#5.7.1)