Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need code to send information or data from the program to specific email addresses.
That means I need code in my project when the customer fills the information or data required and presses "send" the information or data received is send to my email addresses.
Please I need to your help quick because this problem has a connection with my job in the Company.
Thanks in advance
Posted
Updated 8-Jan-11 4:49am
v3

Under normal circumstances, "I need it quick" and "Gimme code" will result in your waiting a long time, or just getting abuse.

But, today, I'm feeling generous. Here is my generic Email routine:
C#
/// <summary>
/// Send an email from [DELETED]
/// </summary>
/// <param name="to">Message to address</param>
/// <param name="body">Text of message to send</param>
/// <param name="subject">Subject line of message</param>
/// <param name="fromAddress">Message from address</param>
/// <param name="fromDisplay">Display name for "message from address"</param>
/// <param name="credentialUser">User whose credentials are used for message send</param>
/// <param name="credentialPassword">User password used for message send</param>
/// <param name="attachments">Optional attachments for message</param>
public static void Email(string to,
                         string body,
                         string subject,
                         string fromAddress,
                         string fromDisplay,
                         string credentialUser,
                         string credentialPassword,
                         params MailAttachment[] attachments)
    {
    string host = ConfigurationManager.AppSettings["SMTPHost"];
    body = UpgradeEmailFormat(body);
    try
        {
        MailMessage mail = new MailMessage();
        mail.Body = body;
        mail.IsBodyHtml = true;
        mail.To.Add(new MailAddress(to));
        mail.From = new MailAddress(fromAddress, fromDisplay, Encoding.UTF8);
        mail.Subject = subject;
        mail.SubjectEncoding = Encoding.UTF8;
        mail.Priority = MailPriority.Normal;
        foreach (MailAttachment ma in attachments)
            {
            mail.Attachments.Add(ma.File);
            }
        SmtpClient smtp = new SmtpClient();
        smtp.Credentials = new System.Net.NetworkCredential(credentialUser, credentialPassword);
        smtp.Host = host;
        smtp.Send(mail);
        }
    catch (Exception ex)
        {
        StringBuilder sb = new StringBuilder(1024);
        sb.Append("\nTo:" + to);
        sb.Append("\nbody:" + body);
        sb.Append("\nsubject:" + subject);
        sb.Append("\nfromAddress:" + fromAddress);
        sb.Append("\nfromDisplay:" + fromDisplay);
        sb.Append("\ncredentialUser:" + credentialUser);
        sb.Append("\ncredentialPasswordto:" + credentialPassword);
        sb.Append("\nHosting:" + host);
        ErrorLog(sb.ToString(), ex.ToString(), ErrorLogCause.EmailSystem);
        }
    }
"UpgradeEmailFormat" and "ErrorLog" are generic routines; you don't need the former, and can write the later yourself!
"MailAttachment" is a simple class you only need if you want to send attachments:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Net.Mime;
using System.Net.Mail;

public class MailAttachment
    {
    #region Fields
    private MemoryStream stream;
    private string filename;
    private string mediaType;
    #endregion
    #region Properties
    /// <summary>
    /// Gets the data stream for this attachment
    /// </summary>
    public Stream Data { get { return stream; } }
    /// <summary>
    /// Gets the original filename for this attachment
    /// </summary>
    public string Filename { get { return filename; } }
    /// <summary>
    /// Gets the attachment type: Bytes or String
    /// </summary>
    public string MediaType { get { return mediaType; } }
    /// <summary>
    /// Gets the file for this attachment (as a new attachment)
    /// </summary>
    public Attachment File{ get {return new Attachment(Data, Filename, MediaType); } }
    #endregion
    #region Constructors
    /// <summary>
    /// Construct a mail attachment form a byte array
    /// </summary>
    /// <param name="data">Bytes to attach as a file</param>
    /// <param name="filename">Logical filename for attachment</param>
    public MailAttachment(byte[] data, string filename)
        {
        this.stream = new MemoryStream(data);
        this.filename = filename;
        this.mediaType = MediaTypeNames.Application.Octet;
        }
    /// <summary>
    /// Construct a mail attachment from a string
    /// </summary>
    /// <param name="data">String to attach as a file</param>
    /// <param name="filename">Logical filename for attachment</param>
    public MailAttachment(string data, string filename)
        {
        this.stream = new MemoryStream(System.Text.Encoding.ASCII.GetBytes(data));
        this.filename = filename;
        this.mediaType = MediaTypeNames.Text.Html;
        }
    #endregion
    }
 
Share this answer
 
Comments
Estys 8-Jan-11 11:18am    
Boy, that *is* generous. Done something bad last year and making amends is your new year resolution?
OriginalGriff 8-Jan-11 11:19am    
Estys, No, :laugh: I had the file open anyway so it was a quick CnP job.
Estys 8-Jan-11 11:25am    
I hope you left some insidious bug :) . The guy has a name like he's the president of Iran but his profile says he's from the US. He's trying to steal our secrets!!!
OriginalGriff 8-Jan-11 11:30am    
Estys, :laugh: I don't have secrets! My life is an open book...
(Well, except the part involving the hollowed out volcano and the InfiniteDeath Laser. But we don't talk about that)
thatraja 8-Jan-11 11:38am    
You are more than enough fair now OG. :-) 5!
 
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