Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I need mail server for gmail for sending contact us information

please kindly update this one which I done.

FRONT END
XML
<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="contactus.aspx.cs" Inherits="contactus" Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    &nbsp;<br />
    <br />
    <asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click" />

</asp:Content>

BACK END
C#
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Mail;
using System.Net.Mail;
using System.Net.Mail.MailMessage;

public partial class contactus : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        MailMessage mail = new MailMessage();
        mail.To.Add("sknsoft1@gmail.com");
        //mail.To.Add("amit_jain_online@yahoo.com");
        //mail.From = new MailAddress("jainamit.agra@gmail.com");
        mail.Subject = "Email using Gmail";

        string Body = "Hi, this mail is to test sending mail" +
                      "using Gmail in ASP.NET";
        mail.Body = Body;

        mail.IsBodyHtml = true;
        SmtpClient smtp = new SmtpClient();
        smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
        smtp.Credentials = new System.Net.NetworkCredential
             ("yourid@gmail.com", "youpassowrd");
        //Or your Smtp Email ID and Password
        smtp.EnableSsl = true;
        smtp.Send(mail);
    }
    protected void to_TextChanged(object sender, EventArgs e)
    {

    }
}


I AM GETTING THIS ERROR:
1. A using namespace directive can only be applied to namespace; 'System.Net.MailMessage' is a type not a name space

Addition from comments:
if i remove it i am getting this errors:
Error   1   'MailMessage' is an ambiguous reference between 'System.Web.Mail.MailMessage' and 'System.Net.Mail.MailMessage' F:\web sites\help php\contactus.aspx.cs 22  9   F:\web sites\help php\
Warning 2   'System.Web.Mail.MailMessage' is obsolete: 'The recommended alternative is System.Net.Mail.MailMessage. http://go.microsoft.com/fwlink/?linkid=14202'   F:\web sites\help php\contactus.aspx.cs 22  9   F:\web sites\help php\
Error   3   'MailMessage' is an ambiguous reference between 'System.Web.Mail.MailMessage' and 'System.Net.Mail.MailMessage' F:\web sites\help php\contactus.aspx.cs 22  32  F:\web sites\help php\
Warning 4   'System.Web.Mail.MailMessage' is obsolete: 'The recommended alternative is System.Net.Mail.MailMessage. http://go.microsoft.com/fwlink/?linkid=14202'   F:\web sites\help php\contactus.aspx.cs 22  32  F:\web sites\help php\
Error   5   'string' does not contain a definition for 'Add'    F:\web sites\help php\contactus.aspx.cs 23  17  F:\web sites\help php\
Error   6   The name 'to' does not exist in the current context F:\web sites\help php\contactus.aspx.cs 23  21  F:\web sites\help php\
Error   7   The name 'from' does not exist in the current context   F:\web sites\help php\contactus.aspx.cs 24  37  F:\web sites\help php\
Error   8   The name 'subject' does not exist in the current context    F:\web sites\help php\contactus.aspx.cs 25  24  F:\web sites\help php\
Error   9   The name 'body' does not exist in the current context   F:\web sites\help php\contactus.aspx.cs 26  21  F:\web sites\help php\
Error   10  'System.Web.Mail.MailMessage' does not contain a definition for 'IsBodyHtml'    F:\web sites\help php\contactus.aspx.cs 27  14  F:\web sites\help php\
Error   11  The name 'yourid' does not exist in the current context F:\web sites\help php\contactus.aspx.cs 31  61  F:\web sites\help php\
Error   12  The name 'yourpassword' does not exist in the current context   F:\web sites\help php\contactus.aspx.cs 31  69  F:\web sites\help php\
Error   13  The best overloaded method match for 'System.Net.Mail.SmtpClient.Send(System.Net.Mail.MailMessage)' has some invalid arguments  F:\web sites\help php\contactus.aspx.cs 32  9   F:\web sites\help php\
Error   14  Argument '1': cannot convert from 'System.Web.Mail.MailMessage' to 'System.Net.Mail.MailMessage'    F:\web sites\help php\contactus.aspx.cs 32  19  F:\web sites\help php\
Posted
Updated 2-Jan-12 6:39am
v6
Comments
anushripatil 28-Dec-11 4:36am    
check out the post
http://www.codeproject.com/Questions/306974/problem-with-g-mail-smpt-server
Richard MacCutchan 28-Dec-11 4:40am    
You did notice that you posted your Gmail userid and password in your code sample here, didn't you?
manoharnch 28-Dec-11 4:42am    
yes sir i have done it in my code i have edited my email id and password, but i am getting this error :
1. A using namespace directive can only be applied to namespace; 'System.Net.MailMessage' is a type not a name space
uspatel 28-Dec-11 4:53am    
remove using System.Net.Mail.MailMessage;
System.Net.Mail works well.
uspatel 28-Dec-11 4:52am    
using System.Net.Mail.MailMessage; is not needed.

try
C#
MailMessage mail = new MailMessage();
        mail.To.Add(to);
        mail.From = new MailAddress(from);
        mail.Subject = subject;
        mail.Body = body;
        mail.IsBodyHtml = true;
        SmtpClient smtp = new SmtpClient("smtp.gmail.com",587); // add port 587
        smtp.EnableSsl = true;
        smtp.UseDefaultCredentials = false;
        smtp.Credentials = new System.Net.NetworkCredential(yourid, yourpassword);
        smtp.Send(mail);
 
Share this answer
 
Add
using System.Net;
using System.Net.Mail;
C#
try
{
SmtpClient client = new SmtpClient("smtp.gmail.com");
MailMessage message = new MailMessage("your's e-mail id(xyz@gmail.com)","e-mail id of the person you want to send mail" );
message.Body = "Body";
message.Subject = "Subject";
client.Credentials = new System.Net.NetworkCredential("Your gmail login(xyz)", "gmail password");
client.Port = Convert.ToInt32("587");
client.Send(message);
MessageBox.Show("Mail delivered successfully!!!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show("Message could not be sent.\nThe following error was returned:\n’" + ex.Message + "‘", "Sending Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

this is working perfectly on my system.
 
Share this answer
 
v2
In your code, remove the last using statement:
C#
using System.Net.Mail.MailMessage;


ADDITION:

Either exclude one of the ...Mail namespaces (the one you don't need or use Fully qualified classes in your code, like:
C#
protected void Button1_Click(object sender, EventArgs e)
    {
        System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
...
 
Share this answer
 
v2
thank you sir
it is very very useful

it is working well

Try
            Dim client As New SmtpClient("smtp.abcdhotels.com")
            Dim message As New MailMessage("reservations@abcdhotels.com", "msbabu143@gmail.com")
            message.Body = "Body"
            message.Subject = "Subject"
            client.Credentials = New System.Net.NetworkCredential("reservations@abcdhotels.com", "111111111111")
            client.Port = Convert.ToInt32("25")
            client.Send(message)
            MessageBox.Show("Mail delivered successfully!!!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Catch ex As Exception
            MessageBox.Show("Message could not be sent." & vbLf & "The following error was returned:" & vbLf & "’" + ex.Message & "‘", "Sending Failed", MessageBoxButtons.OK, MessageBoxIcon.[Error])
        End Try
 
Share this answer
 

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