代码发送信息 [英] code send information

查看:83
本文介绍了代码发送信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要代码将程序中的信息或数据发送到特定的电子邮件地址.
这意味着,当客户填写所需的信息或数据并按发送"键时,在接收到的信息或数据发送到我的电子邮件地址后,我需要在项目中使用代码.
请尽快为您提供帮助,因为此问题与我在公司的工作有关.
在此先感谢

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

推荐答案

在正常情况下,我需要快速"和"Gimme代码"将导致您等待很长时间,或者只是被滥用. br/>
但是,今天,我感到很慷慨.这是我的通用电子邮件例程:
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:
/// <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"和"ErrorLog"是通用例程;您不需要前者,可以自己编写后者!
"MailAttachment"是一个简单的类,仅在您要发送附件时才需要:

"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:

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
    }


这应该有所帮助:
http://openmapi.org/nmapi [ ^ ]

或这样:
http://stackoverflow.com/questions/784997/launching-email- application-mapi-from-c-with-attachment [ ^ ]

问候
Espen Harlinn
This should help:
http://openmapi.org/nmapi[^]

or this:
http://stackoverflow.com/questions/784997/launching-email-application-mapi-from-c-with-attachment[^]

Regards
Espen Harlinn


这篇关于代码发送信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆