Click here to Skip to main content
15,908,675 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to sent mails from my application, from address may be any domain and to address also any domain.But in my application i already registered my domain name(mail.bsl.co.uk) in web.config file.the mails are going from that domain(xyz@bsl.co.uk) to other domains.But i want to sent mails from gmail,yahoo etc to other domains.
Posted

Hi ,
check this
C#
string from ="test@gmail.com"; //Replace this with your own correct Gmail Address

        string to = "test1@gmail.com";//Replace this with the Email Address to whom you want to send the mail

        System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
        mail.To.Add(to);
        mail.From = new MailAddress(from, "One Ghost", System.Text.Encoding.UTF8);
        mail.Subject = "This is a test mail";
        mail.SubjectEncoding = System.Text.Encoding.UTF8;
        mail.Body = "This is Email Body Text";
        mail.BodyEncoding = System.Text.Encoding.UTF8;
        mail.IsBodyHtml = true;
        mail.Priority = MailPriority.High;
 
        SmtpClient client = new SmtpClient();
        //Add the Creddentials- use your own email id and password

        client.Credentials = new System.Net.NetworkCredential(from, "********");
 
        client.Port = 587; // Gmail works on this port
        client.Host = "smtp.gmail.com";
        client.EnableSsl = true; //Gmail works on Server Secured Layer
        try
        {
            client.Send(mail);
        }
        catch (Exception ex)
        {
            Exception ex2 = ex;
            string errorMessage = string.Empty;
            while (ex2 != null)
            {
                errorMessage += ex2.ToString();
                ex2 = ex2.InnerException;
            }
            HttpContext.Current.Response.Write(errorMessage);
        } // end try 

Best Regards
M.Mitwalli
 
Share this answer
 
 
Share this answer
 
v2
 
Share this answer
 
v3
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
namespace MyMail
{
    class SendMail
    {
        SmtpClient objSmtpClient = new SmtpClient();
        MailMessage objMailMessage = new MailMessage();
        MailAddress objMailAddress;
        public SendMail()
        {
           // objSmtpClient.Host = "";
           // objSmtpClient.Port = ;
            objSmtpClient.Host = "smtp.gmail.com";
            objSmtpClient.Port = 465;
        }
       public  bool Sendmail(string mail_From,string mail_To,string mail_Password,string mail_Subject,string mail_Message)
        {
            objMailAddress = new MailAddress(mail_From);
            System.Net.NetworkCredential objNetworkCredential = new System.Net.NetworkCredential(mail_From, mail_Password);
            objSmtpClient.UseDefaultCredentials = false;
            objSmtpClient.Credentials = objNetworkCredential;
            objMailMessage.From = objMailAddress;
            objMailMessage.Subject = mail_Subject;
            objMailMessage.Body = mail_Message;
            objMailMessage.IsBodyHtml = false;
            objMailMessage.To.Add(mail_To);
            try
            {
                objSmtpClient.Send(objMailMessage);
                    return true;
            }
            catch (Exception e)
            {
                return false; 
            }
        }
    }
}


make necessary changes for the above code.
 
Share this answer
 
v2
Please refer following url,

Send mail using Google Apps[^]

Hope this may help you.
 
Share this answer
 
C#
protected void SendMail(object sender, EventArgs e)
    {

            System.Net.Mail.MailAddress fromAddress = new System.Net.Mail.MailAddress(@ConfigurationSettings.AppSettings["EmailFrom"]);
            smtpClient.Host = @ConfigurationSettings.AppSettings["MailServerName"];
            smtpClient.Port = Convert.ToInt32(@ConfigurationSettings.AppSettings["Port"]);
            message.From = fromAddress;
            message.To.Add(@ConfigurationSettings.AppSettings["EmailTo"]);
            message.Subject = @ConfigurationSettings.AppSettings["EmailSubject"];
            message.IsBodyHtml = false;
            message.Body = " Any Message Text.....";
            smtpClient.Send(message);
        }
        finally
        {
            sw.Close();
        }
    }

  <connectionStrings>
  <appSettings>


    <add key="Port" value="587"/>
    <add key="MailServerName" value="smtp.gmail.com"/>
    <add key="EmailFrom" value="emailfrom@gmail.com"/>
    <add key="EmailPassword" value="pass@123"/>
    <add key="EmailTo" value="emailto@gmail.com"/>
    <add key="EmailSubject" value="My Email Subject"/>

  </appSettings>
  <connectionStrings/>
 
Share this answer
 
v2
Hi
Check This link it may helps you..

Send mail in Asp.net

Send mail with multiple attachments

let me know..!
 
Share this answer
 
Comments
dcba1 17-Apr-12 5:19am    
I just want query form in html that is send to my id when user enter something and click on submit button.
Developers Code 17-Apr-12 6:21am    
Sorry to say i didn't get you...I think you are asking about "When user enters the data in textbox(Message) it will display in a form and that form will be send to your email Id ri8..!" ...Pls elaborate the question..

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