我想出了如何做到这一点,并没有像它出现那么困难。 建立在你的web服务,命名空间中的以下内容: `
[Serializable]
public class MyAttachment
{
[DataMember]
public string Name { get; set; }
[DataMember]
public byte[] Bytes { get; set; }
}`
然后将以下添加到您的网络的方法参数: MyAttachment[] attachment
添加以下在你的web-方法的执行块:`
foreach (var item in attachment)
{
Stream attachmentStream = new MemoryStream(item.Bytes);
Attachment at = new Attachment(attachmentStream, item.Name);
msg.Attachments.Add(at);
}`
创建以下属性(或类似的东西),在您的客户端: `
private ObservableCollection<ServiceProxy.MyAttachment> _attachmentCollection;
public ObservableCollection<ServiceProxy.MyAttachment> AttachmentCollection
{
get { return _attachmentCollection; }
set { _attachmentCollection = value; NotifyOfPropertyChange(() => AttachmentCollection); }
}`
在构造函数中新建公共属性(AttachmentCollection)。 添加以下在您打开文件对话框应该返回文件:`
如果,你打电话给你的网络的方法来发送电子邮件(openFileDialog.File!= NULL)
{
foreach (FileInfo fi in openFileDialog.Files)
{
var tempItem = new ServiceProxy.MyAttachment();
tempItem.Name = fi.Name;
var source = fi.OpenRead();
byte[] byteArray = new byte[source.Length];
fi.OpenRead().Read(byteArray, 0, (int)source.Length);
tempItem.Bytes = byteArray;
source.Close();
AttachmentCollection.Add(tempItem);
}
}`
于是最后,添加以下(或类似的东西):
MailSvr.SendMailAsync(FromAddress, ToAddress, Subject, MessageBody, AttachmentCollection);
这个工作对我来说,附件与邮件发送,所有数据完全一样的原始文件。
这工作,但我切换到“lesnikowski”来处理我的邮件,不得不承认的人,mail.dll运行良好,而且很容易实现。 – Deceased 2011-08-01 07:08:15