简体   繁体   中英

Can i use IP Address as SMTP host rather then smtp.email.com

i am creating an Email sending sample application, and i want to use send email from different email address like "gmail, yahoo, hotmail" so i don't want to use "smtp.email.com" as host, because if i use "smtp.email.com" as host i will have to change my host name for every different company like("smtp.gmail.com" for gmail or "smtp.mail.yahoo.com" for yahoo.com ) so Can i use IP Address as SMTP host rather then smtp.email.com.

Please give me a solution for this so that without changing smtp host name i can use different email company to send email.

this is my code:

        try
        {
            // setup mail message
            MailMessage message = new MailMessage();
            message.From = new MailAddress(textBox1.Text);
            message.To.Add(new MailAddress(textBox2.Text));
            message.Subject = textBox3.Text;
            message.Body = richTextBox1.Text;

            // setup mail client
            SmtpClient mailClient = new SmtpClient("smtp.gmail.com");//here i have to change SMTP host for different email company 
            mailClient.Credentials = new NetworkCredential(textBox1.Text,"password");

            // send message
            mailClient.Send(message);

            MessageBox.Show("Sent");
        }
        catch(Exception)
        {
            MessageBox.Show("Error");
        }

Sure you could use IP addresses instead of names, but remember then if they ever changed the IP you're goning to stop working.. BUT.... this needs to change depending on what you are sending the mail as unless you find some form of relay proxy thats open.. AS yahoo wont recveive gmail and gmail wont receive yahoo etc.. The reality is if you are sending as that it would end up changing wether you used an IP or a name.

Your webserver however will most likely send mails from your domain, rather than your gmail/yahoo accounts.. why not send it from your domain? eg noreply@myweb.com then the smtp server remains the same as its your web provider

Of course you could do

SmtpClient mailClient
if (textbox1.Text.Contains("gmail") 
{
            mailClient = new SmtpClient("smtp.gmail.com");/
            mailClient.Credentials = new NetworkCredential(textBox1.Text,"password"); 
}
else if (textbox1.Text.Contains("somemail") 
{
            mailClient = new SmtpClient("smtp.somemail.com");/
            mailClient.Credentials = new NetworkCredential(textBox1.Text,"password"); 
}

etc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM