Can't sent email exchange RPC HTTP - c#

Ok so I tried in so many ways to sent this email but i just cannot get, this are the only two given by provider of the service
the url of the exchange Server
EXVMBX016-3.exch016.msoutlookonline.net
proxy to connect over http
dc016-8.exch016.msoutlookonline.net
NOTE: I tried with SSL
Now my problem it's that in the default port which i'm understand it's 25 the connection it's refused so i opened my outlook and with a sniffer i tried to see what's the port opened in that url, it was 135 give's a timeout.
System.Net.NetworkCredentialnetworkCredentials = new System.Net.NetworkCredential("example#example.com", "*****");
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.To.Add("example#example.com");
message.Subject = "Esto es una prueba de direccion";
message.From = new System.Net.Mail.MailAddress("example#example.com");
message.Body = "Esto es una prueba del servicio de direcciones";
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("EXVMBX016-3.exch016.msoutlookonline.net");
//smtp.Port = 135
//smtp.EnableSsl = False
smtp.UseDefaultCredentials = false;
smtp.Credentials = networkCredentials;
smtp.Send(message);
So If Do i miss something?. or What am I doing wrong?
UPDATE
Here's some images of my configuration on outlook

Your problem is that you are using wrong SMTP Address. Based on my understanding an Exchange Server name can not be SMTP name cEXVMBX016-3.exch016.msoutlookonline.net. Your SMTP Server Address will be different then what have you used above and that's why the connection is refused. When sending any email you just need correct SMTP address and proper credentials to connect it, that's it. I believe your code is correct.
To get correct SMTP address, open your email address contact card in Outlook and then look for "Email Address" properties and there you will get correct SMTP address and use in above code.
[Added Later after above images are added]
Based on above images, you are using Exchange Server Address as SMTP address which is wrong. You must get the correct SMTP address from your IT and then use that to send your email. Your code is right so correct SMPT address will work immediately.

Related

Cannot connect to remote server while sending mail

I'm using code below to send an email and it just doesn't work and gives an exception:
MailDefinition md = new MailDefinition();
md.IsBodyHtml = true;
md.From = EMAIL_FROM;
md.Subject = "لینک تغییر رمز";
MailMessage mm = md.CreateMailMessage(to, null, new System.Web.UI.Control());
string body = link;
mm.Body = body;
SmtpClient smtp = new SmtpClient();
smtp.UseDefaultCredentials = false;
smtp.Host = EMAIL_SMTP;
smtp.Port = EMAIL_PORT;
smtp.Credentials = new System.Net.NetworkCredential(EMAIL_FROM, EMAIL_PASSWORD);
smtp.Send(mm);
where sender username & password are checked and correct, port is 25 and smtp is mail.kawp.co.ir, on send function it throws an exception with the below error message
{"Unable to connect to the remote server"}
{"A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond xx.xxx.xx.xx:25"}
I'm using the same method and function for my other domains which are hosted on the same server and they are working fine. What possibly could be causing the problem here?
p.s: I also can't handshake with the mail.kawp.co.ir 25 via telnet
Thanks in advance
Changing port 25 to 587 fixed my problem because my isp is blocking port 25 to prevent spamming.
If you cannot perform the handshake with Telnet, then you're not going to get it to connect programmatically. From what it sounds like, the server isn't configured to accept SMTP messages. You'll need to get that enabled before you can get your code to work.

Mailbox unavailable. The server response was: relay not permitted

I am sending emails via an external SMTP server. Sending the email is handled with this code:
try
{
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
mail.From = new MailAddress(froma);
mail.To.Add(toc);
mail.Subject = subject;
mail.IsBodyHtml = true;
mail.Body = body;
SmtpClient smtp = new SmtpClient(ClientServer);
DataSet ds = new DataSet();
int retCode = Email.getSmtp(ref ds, DatabaseName);
string User="";
string Password="";
if (ds.Tables["Value"].Rows.Count > 0)
{
User = ds.Tables["Value"].Rows[0]["UserName"].ToString();
Password = ds.Tables["Value"].Rows[0]["PasswordName"].ToString();
}
else
{
MessageBox.Show("Invalid SMTP settings!");
return;
}
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential(User, Password);
smtp.EnableSsl = false;
smtp.Send(mail);
}
catch (System.Web.HttpException exHttp)
{
System.Console.WriteLine("Exception occurred:" + exHttp.Message);
}
Testing this code on my server, with my own SMTP server on the same network, this returns all my emails. However, using an external SMTP server causes the error:
Mailbox unavailable. The server response was: relay not permitted.
I have read around and it appears that the admin for SMTP must allow relays for my server. However, using the authentication credentials provided, I can't seem to connect, and am still receiving the relay error.
Yes, it sounds like the external server that you are using is not allow relay. Even if you have the proper authentication credentials, you will not be able to send the email because the relay function is still disabled. Are you the admin of this external server? If you are then you can enable it. This LINK HERE explains how to set up SMTP and the relay. If you are not the admin of this external server, then you will have to contact who is so they can enable the SMTP and relay for you.
It sounds like the server on your network has SMTP installed and the relay is set up properly since you are able to send. I had to install SMTP and configure the relay on all three of servers here (development box, staging box, and production box) to send emails. I hope this information helps.
I had the same error and commented out :
//smtp.UseDefaultCredentials = true;
And the email was sent successfully.

The SMTP server requires a secure connection or the client was not authenticated - Error

I am trying to send email using Amazon SES.
I am able to send email by using our local SMTPserver and also able to send email by using sample provided at amazon website.
I need to send send From Address & To Address names with the email. I can not do this with SendEmailRequest class provided in the Amazon SDK, because there is no such overload for WithSource(toaddress), WithDestination(destinationaddress) & WithReplyToAddresses(replytoaddress) methods so i can't pass names form sender 7 receiver here, so that I am using regular method of sending mail using Amazon configurations.
I tried both way pass credentials by the hard coding through code as well as by puyting configuration by file but still I am getting same error for both ways above this error when using port 587,
"The SMTP server requires a secure connection or the client was not authenticated. The server response was: Authentication required"
when tried with 465 port getting this error,
"Failure Sending Email"
When tried putting IP address instead of host address of amazon server got this error.
"The remote certificate is invalid according to the validation procedure."
Please suggest me what I am missing here,
Here is my code,
MailMessage mail = new MailMessage();
mail.From = new System.Net.Mail.MailAddress(FromEmail, FromName);
SmtpClient smtp = new SmtpClient("email-smtp.us-east-1.amazonaws.com", 587);
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.UseDefaultCredentials = false;
smtp.EnableSsl = true;
smtp.Credentials = new NetworkCredential(AWSAccessKey, AWSSecretKey);
//recipient address
mail.To.Add(new MailAddress(ToEmail, ToName));
//Formatted mail body
mail.IsBodyHtml = true;
mail.Body = strBody;
smtp.Send(mail);
Thanks in Advance..!!!
I solved this problem by passing email with email user name in the following in the format
User Name< example#domain.com >
Sample from Amazon site is already working for me,
here is my working code,
AWSCredentials objAWSCredentials = new BasicAWSCredentials(AWSAccessKey, AWSSecretKey);
Destination destination = new Destination().WithToAddresses(new List<string>() { TO });
// Create the subject and body of the message.
Content subject = new Content().WithData(SUBJECT);
Content textBody = new Content().WithData(BODY);
Body body = new Body().WithHtml(textBody);
//Body body = new Body().WithText(textBody);
// Create a message with the specified subject and body.
Message message = new Message().WithSubject(subject).WithBody(body);
// Assemble the email.
SendEmailRequest request = new SendEmailRequest().WithSource(FROM).WithDestination(destination).WithMessage(message).WithReplyToAddresses(REPLYTO);
// Instantiate an Amazon SES client, which will make the service call. Since we are instantiating an
// AmazonSimpleEmailServiceClient object with no parameters, the constructor looks in App.config for
// your AWS credentials by default. When you created your new AWS project in Visual Studio, the AWS
// credentials you entered were added to App.config.
AmazonSimpleEmailServiceClient client = new AmazonSimpleEmailServiceClient(objAWSCredentials);
// Send the email.
Console.WriteLine("Attempting to send an email through Amazon SES by using the AWS SDK for .NET...");
client.SendEmail(request);
here I've passed FROM, TO & ReplyToAddress in this format,
User Name< example#domain.com >

Using System.Net.Mail.SmtpClient, change the SMTP FROM email address

As it says in the title, I wish to change the FROM address provided to the SMTP server, as opposed to the FROM address in the email envelope.
The closest sample I can find is from Java, which is can be found here
Thanks
Bottom line is, you can't do this. The FROM address used in System.Net.Mail is used for both the SMTP transaction (Envelope-From) and the MailMessage from header value.
Sorry,
Dave
The FROM provided to the SMTP server is the login of the SmtpClient while the one in the Mail is the FROM in the MailMessage.
SmtpClient smtp = new SmtpClient();
smtp.Host = "myserver.address.com";
smtp.Credentials = new NetworkCredential("me#server.com", "myPassword");
MailMessage msg = new MailMessage();
msg.From = "otherMe#server.com";
//OTHER MESSAGE SETTINGS
smtp.Send(msg);
This should send an e-mail from "otherMe#server.com" using the authentication on the server for the user "me#server.com"
The Java example is about return address, not From.
As Far as I know, you can't do this. SMTP servers use the From address to decide if the want to relay or not.
The only other credential you've got is the Login to the SMTP server.
Unfortunately, this is not possible.
First there is a syntax error for smtp.Host = smtp.serv.com; This is not valid written string type, and the second thing is that the property Host doesn't exist.
As explained by bzlm, if you're using Network delivery method of SmtpClient, then you can set MAIL FROM by setting the MailMessage.Sender property. Note, however, that this will have the side-effect of adding a Sender heady to your message, which will cause many email clients to display present the message sender as "X on behalf of Y".

Sending an email with the header return-path using windows virtual mail server

I'm trying to send an email message using the .NET MailMessage class which can also have the return-path header added so that any bounces come back to a different email address. Code is below:
MailMessage mm = new MailMessage(
new MailAddress(string.Format("{0}<{1}>", email.FromName, email.FromEmail)),
new MailAddress(emailTo));
mm.Subject = ReplaceValues(email.Subject, nameValues);
mm.ReplyTo = new MailAddress(string.Format("{0}<{1}>", email.FromName, email.FromEmail));
mm.Headers.Add("Return-Path", ReturnEmail);
// Set the email html and plain text
// Removed because it is unneccsary for this example
// Now setup the smtp server
SmtpClient smtp = new SmtpClient();
smtp.Host = SmtpServer;
smtp.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
if (SmtpUsername.Length > 0)
{
System.Net.NetworkCredential theCredential =
new System.Net.NetworkCredential(SmtpUsername, SmtpPassword);
smtp.Credentials = theCredential;
}
smtp.Send(mm);
Whenever I check the email that was sent I check the header and it always seems to be missing return-path. Is there something I am missing to configure this correctly? As I said above I'm using the standard Virtual Mail Server on my development machine (XP) however it will run on Windows 2003 eventually.
Has anyone got any ideas why it isn't coming through?
The Return-Path is set based on the SMTP MAIL FROM Envelope. You can use the Sender property to do such a thing.
Another discussion on a related issue you will have sooner or later: How can you set the SMTP envelope MAIL FROM using System.Net.Mail?
And btw, if you use SmtpDeliveryMethod.PickupDirectoryFromIis, the Sender property is not used as a MAIL FROM; you have to use Network as a delivery method to keep this value.
I did not find any workaround for this issue.
PickupDirectoryFromIis, Sender property and SMTP MAIL FROM envelope

Categories