How Can i Send Mail Through Exchange Server by using SMTP - asp.net

I want to Run Below code without
NetworkCredential nc = new Net.NetworkCredential("USERNAME", "PASSWORD").
BY using Only Exchange Host (Server Name) And Port
Im Getting Error For this code : Mailbox unavailable. The server response was: 5.7.1 Client does not have permissions to send as this sender
protected void SendEmail(object sender, EventArgs e)
{
SmtpClient smtpClient = new SmtpClient("ExchangeServerName",25);
MailMessage message = new MailMessage();
try
{
MailAddress fromAddress = new MailAddress("bala#OfficeName.com", "From Me");
MailAddress toAddress = new MailAddress("bala#OfficeName.com", "To You");
message.From = fromAddress;
message.To.Add(toAddress);
message.Subject = "Testing!";
message.Body = "This is the body of a sample message";
smtpClient.UseDefaultCredentials = true;
System.Net.NetworkCredential nc = CredentialCache.DefaultNetworkCredentials;
smtpClient.Credentials = (System.Net.ICredentialsByHost)nc.GetCredential("ExchangeServerName", 25, "Basic");
smtpClient.Send(message);
lblText.Text ="Email sent.";
}
catch (Exception ex)
{
lblText.Text = "Coudn't send the message!\n " + ex.Message;
}
}

I have done it. For more details about my code use this link.
Below Code is worked Fine with
Server : Windows Server 2003,Windows Server 2008,Windows Server 2008 R2
IIS : 6.0, 7.0
.Net Frame Wotk : 2.0,3.5,4.0
string sMessage;
SmtpClient smtpClient = new SmtpClient();
MailMessage message = new MailMessage();
try
{
//you can provide invalid from address. but to address Should be valil
MailAddress fromAddress = new MailAddress("bala#technospine.com", "BALA");
smtpClient.Host = "Exchange Server Name";
smtpClient.Port = 25;
//smtpClient.Port = 587;
smtpClient.UseDefaultCredentials = true;
message.From = fromAddress;
message.To.Add(bala#technospine.com); //Recipent email
message.Subject = _subject;
message.Body = _details;
message.IsBodyHtml = true;
//smtpClient.EnableSsl = true;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.Send(message);
sMessage = "Email sent.";
}
catch (Exception ex)
{
sMessage = "Coudn't send the message!\n " + ex.Message;
}
lblMailStatus.Text = sMessage;

You are attempting to send a mail message using Exchange. In order to do that, the sender (or sending process) must have permissions on the account it is logged in under to send on behalf of the user you are specifying as the sender. This is different from going through Exchange's SMTP mail transfer agent (MTA) in order to have Exchange receive and route an email message. So you are on the right track with knowing you should do this using SMTP, but you are just trying to use the wrong API for accomplishing this. You want to take a look at CDOSYS for sending it through the SMTP MTA without having to do user authentication. Search on System.Web.Mail.MailMessage for more specific examples - there are plenty out there. If the Exchange server does not seem to accept/deliver the SMTP message delivered to it in this fashion, you might simply need to open up its configuration a bit. In that event, the Exchange server is probably configured with tight security on routing of mail received via its SMTP MTA and just needs to have the IP address of the machine(s) you are sending these messages from configured to allow for mail forwarding.

try NetworkCredential nc = new Net.NetworkCredential("USERNAME", "PASSWORD","DOMAIN")

Related

Sending Email through SmtpClient smtp.office365.com Generating Error in ASP.Net, C# web forms

Anyone can help please.
I am using the following code for sending email from my ASP.Net, C# web forms application which generating the error. The same code is working for other application. My domain name contains a hyphen in it (-). Is that the reason? Do I need to see email configuration or server/domain level settings? Please help.
string smtpServer = "smtp.office365.com";
int port = 587;
using (MailMessage mail = new MailMessage())
{
using (SmtpClient SmtpServer = new SmtpClient(smtpServer))
{
mail.From = new MailAddress(_emailIDFrom);
mail.To.Add(_emailIDTo);
mail.Subject = _subject;
mail.IsBodyHtml = true;
mail.Body = _emailBody;
SmtpServer.Port = port;
SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network;
SmtpServer.UseDefaultCredentials = false;
SmtpServer.Credentials = new System.Net.NetworkCredential(_emailIDFrom, _emailIDFromPassword);
SmtpServer.EnableSsl = true;
try
{
SmtpServer.Send(mail);
return true;
}
catch (Exception ex)
{
lblMessage.Text = ex.Message;
return false;
}
}
}
My Tls code in Global.asax Application_Start as follows
if (ServicePointManager.SecurityProtocol.HasFlag(SecurityProtocolType.Tls12) == false)
{
ServicePointManager.SecurityProtocol = ServicePointManager.SecurityProtocol | SecurityProtocolType.Tls12;
}
ERROR--
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.57 Client not authenticated to send mail. Error: 535 5.7.139 Authentication unsuccessful, SmtpClientAuthentication is disabled for the Tenant. Visit https://aka.ms/smtp_auth_disabled for more information. [DXXP273CA0023.AREP273.PROD.OUTLOOK.COM]

How to send Email using gmail account in Asp.net WEB API

I am trying to send bulk of emails through smtp server using gmail account, but i am facing issue like this: "The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Must issue a STARTTLS command first. ry1sm18393619pab.30 - gsmtp". I am using Asp.net c# console application.
here is my code which i am using:
static void Main(string[] args)
{
EmailSend send = new EmailSend();
send.Page_Load();
}
protected void Page_Load()
{
try
{
MailMessage msg = new MailMessage(new MailAddress("abc#gmail.com"), new MailAddress("abc#gmail.com"));
msg.Subject = "Test link..";
msg.IsBodyHtml = true;
string emailid = "abc#gmail.com";
string password = "abc123";
string host = "smtp.gmail.com";
SmtpClient smtpclient = new SmtpClient();
smtpclient.Host = host;
smtpclient.EnableSsl = true;
smtpclient.Port = 587;
smtpclient.UseDefaultCredentials = false;
smtpclient.Credentials = new System.Net.NetworkCredential(emailid, password);
smtpclient.Send(msg);
Console.WriteLine("A link has been send to you for resetting password. Check your mail! ");
}
catch (Exception ex)
{
Console.WriteLine("Could not send the e-mail - error: " + ex.Message);
}
}
I don't understand why this issue is coming. I have given correct credentials like: Emailid and password for gmail, i have changed port number also 25,587. If anyone has some sloution then it would be very helpful.

Sending an email with ASP .Net and Gmail SMTP

I am developping an ASP .Net website.
One of my ASPX page contains a button.
When this button is clicked I want to send an email by using the Gmail SMTP server.
I want to use my Gmail account as credentials.
Here is the button's click event handler :
protected void m_myButton_Click(object p_sender, EventArgs p_args)
{
string l_subject = "My subject";
StringBuilder l_builder = new StringBuilder();
// ...
string l_body = l_builder.ToString();
string l_host = ConfigurationManager.AppSettings["SmtpHost"];
int l_port = int.Parse(ConfigurationManager.AppSettings["SmtpPort"]);
NameValueCollection l_smtpAccount = (NameValueCollection)ConfigurationManager.GetSection("smtpAccount");
string l_address = l_smtpAccount["SmtpAddress"];
string l_password = l_smtpAccount["SmtpPassword"];
MailMessage l_mail = new MailMessage();
l_mail.From = new MailAddress(l_address);
l_mail.To.Add(l_address);
l_mail.Subject = l_subject;
l_mail.Body = l_body;
SmtpClient l_smtp = new SmtpClient();
l_smtp.Host = l_host; // l_host = "smtp.gmail.com"
l_smtp.Port = l_port; // l_port = 587
l_smtp.EnableSsl = true;
l_smtp.UseDefaultCredentials = false;
l_smtp.Credentials = new NetworkCredential(l_address, l_password);
l_smtp.Send(l_mail);
}
The command line l_smtp.Send(l_mail) fails.
A SMTP exception is raised and the InnerException property informs me that (translation from French to English) : No connection has been etablished because the target computer has refused it 173.194.67.108:587
My code is based on code found on this page : http://www.codeproject.com/Questions/254743/sending-email-in-asp-net-using-smtp-gmail-server
Any help will be greatly appreciated
Try this...
protected void Button1_Click(object sender, EventArgs e)
{
MailMessage mail = new MailMessage();
mail.To.Add("Email ID where email is to be send");
mail.To.Add("Another Email ID where you wanna send same email");
mail.From = new MailAddress("YourGmailID#gmail.com");
mail.Subject = "Email using Gmail";
string Body = "Hi, this mail is to test sending mail"+
"using Gmail in ASP.NET";
mail.Body = Body;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
smtp.Credentials = new System.Net.NetworkCredential
("YourUserName#gmail.com","YourGmailPassword");
//Or your Smtp Email ID and Password
smtp.EnableSsl = true;
smtp.Send(mail);
}
No connection has been etablished because the target computer has refused it 73.194.67.108:587
Try the other SMTP port: 465
I have turned my antivirus program off as advised by Rup and I have not got any exception.
I will created a custom rule in my antivirus program to allow communication between my ASP .Net website and the Gmail SMTP server.

Sending Mail via Mail Server Asp.net mvc4

I am trying to send mail through mail server ( available in fatcow).
Username: info#efernssolutions.com
SMTP Server: smtp.hostname.com
SMTP Port: 587
I used the follwing code to send mail ,
if (ModelState.IsValid)
{
MailMessage message = new MailMessage();
try
{
message.To.Add(new MailAddress("kishore.eferns#gmail.com"));
message.From = new MailAddress("info#efernssolutions.com");
message.Subject = "Test mail";
message.Body = "this is the mail sended through my c#.net program";
message.BodyEncoding = System.Text.Encoding.UTF8;
message.SubjectEncoding = System.Text.Encoding.UTF8;
SmtpClient client = new SmtpClient();
client.Port = 587;
client.Host = "smtp.hostname.com";
System.Net.NetworkCredential nc = new System.Net.NetworkCredential("info#efernssolutions.com", "password");// is u are using Gmail
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = nc;
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateServerCertificate);
client.Send(message);
}
catch (Exception ex)
{
;
}
}
When i use gmail server the code works fine and i received mail.
But it is not working with my email server.
I get the error,
the remote certificate is invalid according to the validation procedure
Is there anything left to do to make it work?
Please help,
Thank you
As first check I will suggest to check if your SSL certificate is correctly installed on your server

error sending email using ASP.NET web app

I'm trying to send email to my site users (ASP.NET, VS2010), currently I'm using my gmail account for sending email, but I'm getting the following error:
ex = {"Failure sending mail."}
InnerException = {"The remote name could not be resolved: 'smtp#gmail.com'"}
it is my code:
MailMessage mailObj = new MailMessage("mygmailaccount#gmail.com", "myyahooaccount#yahoo.com", "test", "test2");
SmtpClient SMTPServer = new SmtpClient("smtp#gmail.com");
SMTPServer.Credentials = new System.Net.NetworkCredential("mygmailaccount", mygmailpassword);
try
{
SMTPServer.Send(mailObj);
}
catch (Exception ex)
{
string a = ex.Message;
}
what is going wrong here? should I do something in my web.config? how can I find smtp server of my own host?
smtp#gmail.com is incorrect. You probably meant smtp.gmail.com. See the following question for a complete example.
Sending email in .NET through Gmail
you did not set gmail parameters correctly:
SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587);
smtpClient.EnableSsl = true;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.Credentials = new NetworkCredential("myGmailAcconut#gmail.com", "password");

Resources