How to get the SMTP server from email address - c#

I want to send mail automatically by special email account, but now, I only know the email address: tsp.monitor#qorosauto.com , and the password. so do you know how to get the SMTP server. below is my C# code:
SmtpClient client = new SmtpClient();
client.Host = "What is the SMTP Server, I want to get from email address, can you help me";
string account = "tsp.monitor#qorosauto.com";
string password = "Qoros111";
client.Port = 587;
client.EnableSsl = true;
client.Timeout = 100000;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(account, password);

You find the SMTP server of a domain by taking the host part of the email address (qorosauto.com in your example) and looking up the MX record for it.
$ dig +short mx qorosauto.com
10 euq2.qorosauto.com.
5 euq1.qorosauto.com.
The number before the hostname indicate preference - in this case euq1.qorosauto.com is the preferred server to connect to.
Doing this in .Net is not straight-forward, as the answer to this question indicates: How to get mx records for a dns name with System.Net.DNS?
To add to the problems, many ISPs will filter your connection in the firewall and won't let you talk to any SMTP server except the ISPs one, which in turn will relay the mail to the recipient.
Essentially, you want to use your ISPs or organizations SMTP server - not the recipients MX.

Trial and error might get you there..
mail.quorosauto.com
smtp.quorosauto.com
www.quorosauto.com
smtp.gmail.com
If you've exausted every possibility you'll need to provide your own SMTP server. There are a few freebie servers:
http://freesmtpservers.com/
However if you are doing this professionally you probably want to use the SMTP server of your organization so you probably want to ask your boss or a colleague.

You may try mail.orosauto.com or smtp.orosauto.com. Login to you domain account and check mail settings. hopefully you can find the mail server details over there.

To find out the responsible mail server you have to ask the DNS. By using the web for example go to this nslookup page.
Normally you start at the domain server of your provider, but you can also start with a root-server (e.g. 198.41.0.4) and ask for the domain google.com and the query type MX - Mail exchange.
You'll get back a list of responsible dns server. Simply pick one out of the list (e.g. 192.26.92.30) and send the same query again to this server. Rerun this sequence till you'll get a list of type MX. Simply pick one address out of this list (maybe the first, maybe the one with the lost value in preference) and use this to establish your smtp connection.
That's the way how every mail server does its work. Now it is up to you to implement that into your application. ;-)

Related

net_io_connectionclosed error trying to send via office365 in c#

I have looked at the other threads and have so far found no solution.
I get the net_io_connectionclosed error whenever I try to connect via the method below.
I am trying to use an Office 365 smtp relay. I do not have access to the main account or ability to change anything about this email so I cannot check settings or even reset the password.
Any and all help gratefully received.
uname: email address of the relay account (no-reply#[x].com)
I am waiting on a reply to my email to get the onmicrosoft.com address, but it is not mentioned in any of the tutorials at all. As such the uname here is also in the msg as the 'from' address.
pwd: password.
SmtpClient client = new SmtpClient("smtp.office365.com", 587);
client.UseDefaultCredentials = false;
client.EnableSsl = true;
client.Credentials = new System.Net.NetworkCredential(uname, pwd);
client.TargetName = "STARTTLS/smtp.office365.com";
client.Timeout = 10000;
client.Send(msg);
Ok, it turns out that using a relay for this is not possible unless you own the IPs (not supplied by Azure, netregistry etc.)
You have to buy a new mailbox from O365 and use that. Thus defeating the purpose of having a realy unless you run your own server (in which case I wouldn't need a relay as I would use my own smtp services.)
This means if you are running Azure, probably best to avoid O365 for email, other suppliers are far more flexible.
Link to the forum at MS:
https://answers.microsoft.com/en-us/msoffice/forum/msoffice_o365admin-mso_other/how-to-setup-an-smtp-relay-for-use-from-an-azure/5d997c38-bd67-4dbd-ad0d-62488addf9ae

Sending mail from office365 account C# mail headers

I can send mail with a valid Office365 account using my C# web application to a list of opt-in addresses. So it is our own data source with our customers in it. The problem is that it is probably not received by our customers. I have send a mail from the info account to my own office365 account and pasted the headers in mxtoolbox and got a message that my local pc is blacklisted. even when it is using the smtp of office365. We have dkim enabled and spf in the dns. What else should I do to troubleshoot this?
Here is my simple C# code for sending the mail (which works, but might needs additional anti-spam stuff?)
SmtpClient sc = new SmtpClient();
sc.Host = "smtp.office365.com";
sc.Credentials = new System.Net.NetworkCredential(SmtpUsername, SmtpPassword);
sc.Port = 587;
sc.EnableSsl = true;
Should I add my home IP and from the office to the SPF dns entry, even when I am using the smtp of office365?
this is the value for Authentication-Results
dkim=none (message not signed) header.d=none;ouroffice365domain.com; dmarc=none action=none
Is that the cause for getting on the blacklist? Please note that I am not trying to send spam. It is a valid data source where people opted in for. We are just migrated to office365 and need to send a mailing to our customers from our custom web application. We are aware of the limitations (10k per day) and 1 mail every 2 sec.
edit will change this: https://stackoverflow.com/a/23409351/169714 because I just used the mail address and not the first and last name of the customer.
Not having DKIM setup is not going to get you blackisted, having it set up improperly can. Your HOME PC should have nothing to do with SPF, it's the lasting sending IP which should be OFFICE 365 IP. Did you validate your DKIM and SPF by sending an email to mailtest#unlocktheinbox.com or check-auth#verifier.port25.com?
I also don't think your LOCAL PC is blacklisted, it's the IP of your INTERNET Provider that is blacklisted. Unless you have a static IP it will change.

Sending email via exchange server issue. (system.net.mail vs microsoft.exchange.webservices)

I am developing a web application for my company. Part of it requires the internal user to fill out a form, and send an email on this user's behalf. It is all within the company. I searched and found two possible routes old system.net.mail and a more recent microsoft.exchange.webservices, but seems our exchange server requires credentials. I can only get the user's login and his email address login+"#company.com". How can i get this done?
Below are the codes i used smtp (system.net.mail), but it doesnt work.
string[] username =System.Security.Principal.WindowsIdentity.GetCurrent().Name.Split('\\');
string email = username[1] + "#company.com";
MailAddress from = new MailAddress(email);
MailAddress to = new MailAddress("someone#company.com");
MailMessage message = new MailMessage(from, to);
message.Subject = "testmail";
message.Body = "<h>testmail</h>";
message.IsBodyHtml = true;
SmtpClient client = new SmtpClient();
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Send(message);
SmtpClient requires that you either specify the SMTP server directly, or that you have the right application/machine configuration settings for it to detect the server automatically. Your code is choosing the second option implicitly, and I suspect you don't have the right settings in app/machine config. Exchange does NOT support SMTP in its default configuration, afaik, so unless someone familiar with your Exchange server knows SMTP is configured and can give you the right address, SmtpClient is probably out.
Exchange Web Services (aka EWS) is probably your better answer, but it's not really a good one. In order to use exchange web services, you will need one of:
1) The domain, username, and password of the user so that you can pass the right NetworkCredential to EWS. In your case, this would probably mean the user has to enter their password into your form, which may break your requirements.
2) The user that the process is running as (in a web application, the application pool identity for IIS) has to have permissions to send mail as the user in question.
3) If you can use ASP.NET authentication to impersonate the user (this would only be a good approach in a LAN application), then you can effectively fall back to option (2), because now you will be talking to EWS as the user, who obviously will have permission to send mail from their own address.
As you can see, the right approach depends greatly on your Exchange/Active Directory/LAN setup.

What is SMTP Server Address and how to get SMTP Server Address?

i am trying to send email from c# windows application and i need SMTP Server Address to send email but i don't know about SMTP Server Address, what is SMTP Server Address, how to get SMTP Server Address and how to use it.
this is the code:
System.Web.Mail.MailMessage message=new System.Web.Mail.MailMessage();
message.From="from e-mail";
message.To="to e-mail";
message.Subject="Message Subject";
message.Body="Message Body";
System.Web.Mail.SmtpMail.SmtpServer="SMTP Server Address";
System.Web.Mail.SmtpMail.Send(message);
What Mail Server are you trying to use??? usually you can just google SMTP or POP3 or whatever protocal your looking for and it will give you the port, server and all the extra information you need to connect to it.
For example:
http://support.google.com/mail/bin/answer.py?hl=en&answer=13287
First, the System.Web.Mail namespace utilities are marked as 'obsolete' and should not be used. Instead, you should use System.Net. In that namespace there is a 'MailMessage' and an 'SmtpClient' class that will do the job you are trying to do.
Next, an SMTP server is a process that runs on a computer, which, when connected to the inetrnet, can listen for, and respond to, incoming requests which use a particular protocol on a particular port. You can think of an SMTP server as the machine at the post office that sorts and routes the mail to the appropriate mailbox..
An SMTP server has an address, just like everything else on the internet that needs to communicate with anything else. The address is used to send your mail message to the right machine, on the right communication channel. you could think of it as its phone number, and your mail message a text that will be sent to it.
Next, the address you are looking for, last time I checked, was: smtp.gmail.com.
So, considering that you need to stop using System.Web.Mail, and considering that your address may be smtp.gmail.com, here is what your code should look like:
// setup mail message
MailMessage message = new MailMessage();
message.From = new MailAddress("from e-mail");
message.To.Add(new MailAddress("to e-mail"));
message.Subject = "Message Subject";
message.Body = "Message Body";
// setup mail client
SmtpClient mailClient = new SmtpClient("smtp.gmail.com");
mailClient.Credentials = new NetworkCredential("youraccount#gmail.com", "yourGmailPassword");
// send message
mailClient.Send(message);
Also, here is a decent looking article on using gmail as your smtp server:
How to use Gmail as your SMTP server
Also, if gmail does not work for you, you can use the smtp server of your internet provider. They will usually have their smtp address lurking on their website somewhere to be helpful to customers who want to setup their email program. You can also look in Outlook under account settings if you cannot find it somewhere else, if you use anything besides gmail, you should find one there.
Lastly, keep in mind, email cannot be sent without using an smtp server which is willing to receive and dispatch your maill message. In general this is something like gmail, or the smtp server of your internet provider, and the address will normally be: smtp.providername.com. However, gmail, for example, requires your account credentials for the smtp server to allow your message to be received and dispatched.

C# SMTP mail sending usually fails due to lack of credentials?

It seems that when you use SmtpClient class and MailMessage to send emails, everything works fine. And it does send it. However, some servers, such as your business Exchange Server or Gmail.com or whatever services, sometimes reject these emails (because they could be phishing sites or spam sites?)
I'm using this and a lot of places reject the email I believe:
SmtpClient smtp = new SmtpClient
{
Host = smtpClient,
Timeout = 40000, // 40 seconds
DeliveryMethod = SmtpDeliveryMethod.Network
};
How do you work around this? Do you have to use login credentials to some recognized / white-listed server so that it is authorized and trusted email?
I could use something like GMAIL authentication, but my server should be allowed to send emails, it shouldn't have to rely on gmail.
Long ago there were many servers on the Internet that provided the service delivery of e-mail without asking too many questions about who was using the service.
Then came the SPAMMERS!.
A server that was sending mail freely around the world was no longer a benefactor of the internet, but a problem.
Nowadays it is increasingly difficult to find a server that sends mail on the Internet without asking for credentials.
Your e-mail server (smtp.yourbusiness.com or whatever you want) does not trust you.
When you ask it to send an email for you, it wants to know who you are.
SmtpClient smtp = new SmtpClient
smtp.Host = "smtp.yourbusiness.com";
NetworkCredential credentials = new NetworkCredential("your_user_name_on_smtpserver", "your_password_on_smtpserver");
smtp.Credentials = credentials;
are you setting the sender mail address and can your mail server be resolved by reverse dns lookup? Some spam filters distrust mail servers they can't reverse lookup. If you for instance make your mail server send mails using a bogus or foreign mail domain, spam filters will probably pick this up and filter your mail. If you have set up a SPF record for your domain and your sending mail server isn't in that record, mails from that server will also often be filtered by spam filters. Another reason could be an IP address from a range known to be dynamically assigned by internet providers. Mail servers sending from these address ranges are mostly spam bots and are therefore often also filtered.
To send GMail you need to specify a number of settings - yes you'll need to use authentication (GMail wouldn't open up their relay to everyone, or they'd get used for spamming).
If I recall correctly you need to send on TCP port 587, enable SSL, host to mail.google.com and provide a username and password on your SmtpClient to get Gmail to actually send it - neglecting one of those usually generates an email back from them telling you what you've forgotten.
Edit: Just to clarify on the username and password bit; you'd need to create a new NetworkCredential with the username and password for your GMail account, and set the Credentials property of your SmtpClient to that.
If i interpret your question correctly: the messages are sent our from your server, but they are trapped in the spamfilters at the receiving end?
If this is the problem, it has nothing to to with the way you are sending the messages but much more with the content of your message and the characteristics of your server. So it does not matter what credentials you use or if you send it with SmtpClient or any other class. Moving to Gmail does not help and Gmail has a limit to the number of messages you can send.
Spam filters like spam assassin use rules. Match too many: your message is considered spam.
Have a look here for an example:
http://spamassassin.apache.org/tests_3_3_x.html
So make sure your message does not get too high a score and it will go trough.

Categories

Resources