Click here to Skip to main content
15,902,114 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to send mail containing username and password to a particular email id.. how can i do that. is ther any particular code for that.
Posted
Updated 8-Jan-14 23:35pm
v2

try this,



C#
using System.Net.Mail;
using System.Net;


private void SendMail(string targetMail, string shownTargetName, string[] attachmentNames, string Username, string Password, string subject, string body)
    {
        var fromAddress = new MailAddress("support@gmail.com", "MailSendingPrag");
        var toAddress = new MailAddress(targetMail, shownTargetName);
        const string fromPassword = "12345isAbadPassword";
        subject = "Your Subject";
        body = @"Here you can put in" + Username + "and" + Password + "that will appear in the body          multilined and even in <html>        ";
        var smtp = new SmtpClient
        {
            Host = "smtp.google.com",
            Port = 587,
            EnableSsl = true,
            DeliveryMethod = SmtpDeliveryMethod.Network,
            UseDefaultCredentials = false,
            Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
        };

        using (var message = new MailMessage(fromAddress, toAddress)
        {
            Subject = subject,
            Body = body
        }
              )
        {
            foreach (string filePath in attachmentNames)
            {
                Attachment attachMail = new Attachment(filePath);
                message.Attachments.Add(attachMail);
            }

            try
            {
                smtp.Send(message);
                Response.Write("E-Mail sent!");
            }
            catch
            {
                Response.Write("Sending failed, check your internet connection!");
            }
        }
    }


password
 
Share this answer
 
v4
Comments
Member 10500918 9-Jan-14 6:20am    
but i'm getting many errors
Bojjaiah 9-Jan-14 6:53am    
see updated my code... if u have an error post as a comment.. here
C#
using System.Net;
using System.Net.Mail;
             try
            {
                MailMessage mail = new MailMessage();
                SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
 
                mail.From = new MailAddress("me@mydomain.com");
                mail.To.Add("u@urdomain.com");
                mail.Subject = filename;
                mail.Body = "Report";
                Attachment attachment = new Attachment(filename);
                mail.Attachments.Add(attachment);
 
                SmtpServer.Port = 25;
                SmtpServer.Credentials = 
                        new System.Net.NetworkCredential("me", "password");
                SmtpServer.EnableSsl = true;
 
                SmtpServer.Send(mail);
            }
 
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