Click here to Skip to main content
15,888,590 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I used the code below to send email, but only sucessfully sent from gmail to other email address.
I changed host and port, but failed.How can I use other email to send?
C#
private string SendMail(string from, string fromname, string to, string subject, string body, string username, string password, string server)
    {
        
        try
        {
            MailMessage mail = new MailMessage();
            mail.From = new MailAddress(from, fromname);
            mail.To.Add(to);
            mail.Subject = subject;
            mail.Body = body;
            
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com";
            smtp.Port = 587;

            smtp.Credentials = new System.Net.NetworkCredential(username, password);
            smtp.EnableSsl = true;
            smtp.Send(mail);

            return "send ok";
        }
        catch (Exception exp)
        {
            return exp.Message;
        }
    }

    protected void btnConfirm_Click(object sender, EventArgs e)
    {
        SendMail("xxx@gmail.com", "Lucy", "xxxx@gmail.com", "text", "this is a test email", "xxx@gmail.com", "xxx", "smtp.gmail.com");  
    }


I changed host = "smtp.sina.com.cn" or "smtp.sina.com", port = 25. But cannot send email.And nothing in the spam. In the future, I will use some companies's email address to send, so now I need to know what is the way send email from any email. Thank you.
Posted
Updated 26-Oct-13 9:03am
v3

1 solution

ok, in your example, obviously you're authenticating to gmail (somehow)- so, if you change your mail host from gmail to someone/something else, then, you'll need credentials (ie an account) and possibly other permissions on that other mail host, to be able to use them to send mail.

Things were a lot easier in the good-old-days, when almost anyone let you send mail through their service - these days, with spam, not so
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900