Click here to Skip to main content
15,888,733 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
hello .. i am using this code successfully i got answer....but my friend using gmail code security access like sms alart .in that time can i access NetworkCredential ...

C#
 usrname = txt_username.Text;
                string mesg = "";
                mesg = txt_message.Text;
                string sub = txt_sub.Text;
                from = new MailAddress(txt_username.Text);
                to = new MailAddress(address);
                msg = new MailMessage(from, to);
                msg.Subject = sub;
                //msg.Attachments.Add(new Attachment(lbl_Attachment1.Text));
                msg.Body = mesg;
                System.Net.NetworkCredential network = new System.Net.NetworkCredential(txt_username.Text, txt_password.Text);
                smtp.Host = "smtp.gmail.com";
                    smtp.Port = 25;
                    smtp.EnableSsl = tru
e;
                smtp.Credentials = network;
                smtp.Send(msg);
                MessageBox.Show("sended");
Posted
Updated 13-Mar-12 21:50pm
v4
Comments
Sergey Alexandrovich Kryukov 14-Mar-12 2:31am    
Not a question, not clear.
--SA
RAMALINGAM.K 14-Mar-12 2:38am    
thanks for reply... you know "system.net.mail" in .net c# that application i am using
Sergey Alexandrovich Kryukov 14-Mar-12 4:30am    
You see, I cannot see a problem immediately, and you are not explaining what's the problem. I note it's not a question and not clear, but you did not add any explanation.
--SA
RAMALINGAM.K 14-Mar-12 2:44am    
reply me
Sergey Alexandrovich Kryukov 14-Mar-12 4:30am    
I just did. And?..
--SA

this below code works perfectly; try this one

C#
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net.Mail;
using System.Security.Permissions;

public class SendMail
{
    string to;
    string subject;
    string message;
    string frm;
    public SendMail(string to, string subject, string message, string frm)
    {
        this.to = to;
        this.subject = subject;
        this.message = message;
        this.frm = frm;	
	}
    public bool Send()
    {
        //set id and password like this

        //string user = "sdda@gmail.com";
        //string password = "dassadad";

        //or like this
        string user = ConfigurationManager.AppSettings["username"].ToString();
        string password = ConfigurationManager.AppSettings["password"].ToString();
        SmtpClient SmtpServer = new SmtpClient();
        SmtpServer.Credentials = new System.Net.NetworkCredential(user, password);
        SmtpServer.Port = 587;
        SmtpServer.Host = "smtp.gmail.com";
        SmtpServer.EnableSsl = true;
        MailMessage mail = new MailMessage();
        try
        {

            mail.From = new MailAddress(user, "Subject", System.Text.Encoding.UTF8);
            mail.To.Add(new MailAddress(to));
            mail.Subject = subject;
            mail.Body = message;
            mail.IsBodyHtml = true;
            mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
            mail.ReplyTo = new MailAddress(frm);
            SmtpServer.Send(mail);

        }
        catch (Exception err)
        {
            return false;
        }
        return true;
    }
}
 
Share this answer
 
v2
Comments
Richard MacCutchan 14-Mar-12 6:05am    
Use <pre> tags when posting code; use the "code" button to do it automatically.
try this,

MailAddress mailfrom = new MailAddress ( "frommail@gmail.com" );
MailAddress mailto = new MailAddress ( "tomail@gmail.com" );
MailMessage newmsg = new MailMessage ( mailfrom, mailto );

newmsg.Subject = "Subject of Email";
newmsg.Body = "Body(message) of email";

////For File Attachment, more file can also be attached

Attachment att = new Attachment ( "G:\\code.txt" );
newmsg.Attachments.Add ( att );

SmtpClient smtps = new SmtpClient ( "smtp.gmail.com", 587 );
smtps.UseDefaultCredentials = false;
smtps.Credentials = new NetworkCredential ( "mail@gmail.com", "pwd" );
smtps.EnableSsl = true;
smtps.Send ( newmsg );
 
Share this answer
 
v2
Comments
Richard MacCutchan 14-Mar-12 6:04am    
Sorry, wrong place for my reply.

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