在我的Asp.net网站上,我创建了新用户,我要做的就是从网站向用户发送电子邮件,并提供他们的登录详细信息。我创建了一个web服务来发送电子邮件,它工作正常。我想在发送给用户的电子邮件中添加一些HTML样式。
所以我创建了一个HTML电子邮件格式,并将其作为HTML页面保存在项目内的文件夹(资源)中。如何从资源文件夹中读取HTML页面并将其与“BODY”参数中的以下代码一起附加,并将其发送到webservices方法。此外,我如何编辑html文件,以便在将其发送到Web服务之前将其包含在用户登录详细信息中。感谢
C#代码将值传递给webservice方法以发送电子邮件
string subject = "login details";
//call the webservice to send email to the newly created user
ServiceClient service = new ServiceClient();
service.sendEmail(newuseremail, subject, BODY, message, myemail);
答案 0 :(得分:3)
关于如何阅读html页面 - 没有什么特别之处,您必须将其作为简单的文本文件处理并使用StreamReader进行读取。 为了能够对内容进行一些更改,请考虑使用某些模式,这些模式可能不会出现在其他地方的html页面中,例如:“$$ USER $$”,在读取文件后,用User name替换此类出现,或者无论你想要它被替换为什么。 为了能够从服务发送html电子邮件,您必须将属性(如果没有错误的IsBodyHtml)设置为true,则为MailMessage实例。
就是这样。
答案 1 :(得分:1)
您希望以字符串形式阅读HTML文件。我假设您可以在本地访问它。
string strHTML=File.ReadAllText("myfile.html");
的副本
答案 2 :(得分:0)
这是一个包装你需要的东西的课程:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Mail;
/// <summary>
/// Wrapper class for the System.Net.Mail objects
/// </summary>
public class SmtpMailMessage : IDisposable
{
#region declarations
MailMessage Message;
SmtpClient SmtpMailClient;
#endregion
#region constructors
/// <summary>
/// Default constructor for the SmtpMailMessage class
/// </summary>
public SmtpMailMessage()
{
//initialize the mail message
Message = new MailMessage();
Message.Priority = MailPriority.Normal;
Message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
Message.From = new MailAddress("xxx@abc.com");
//initialize the smtp client
SmtpMailClient = new SmtpClient();
SmtpMailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
SmtpMailClient.Host = "192.168.0.1";
SmtpMailClient.Port = 25;
}
/// <summary>
/// Parameterized constructor for the SmtpMailMessage class. Allows for override of the default
/// SMTP host and port number
/// </summary>
/// <param name="HostIP">The IP address of the exchange server</param>
/// <param name="PortNumber">The port number for ingoing and outgoing SMTP messages</param>
public SmtpMailMessage(string HostIP, int PortNumber) : this()
{
//override the smtp host value
SmtpMailClient.Host = HostIP;
//override the smtp port value
SmtpMailClient.Port = PortNumber;
}
#endregion
#region subject / body
/// <summary>
/// The body content of the mail message
/// </summary>
public string Body
{
get
{
return Message.Body;
}
set
{
Message.Body = value;
}
}
/// <summary>
/// the subject of the mail message
/// </summary>
public string Subject
{
get
{
return Message.Subject;
}
set
{
Message.Subject = value;
}
}
#endregion
#region mail type
/// <summary>
/// Gets or sets a value that determines whether the mail message
/// should be formatted as HTML or text
/// </summary>
public bool IsHtmlMessage
{
get
{
return Message.IsBodyHtml;
}
set
{
Message.IsBodyHtml = value;
}
}
#endregion
#region sender
/// <summary>
/// Gets or sets the from address of this message
/// </summary>
public string From
{
get
{
return Message.From.Address;
}
set
{
Message.From = new MailAddress(value);
}
}
#endregion
#region recipients
/// <summary>
/// Gets the collection of recipients
/// </summary>
public MailAddressCollection To
{
get
{
return Message.To;
}
}
/// <summary>
/// Gets the collection of CC recipients
/// </summary>
public MailAddressCollection CC
{
get
{
return Message.CC;
}
}
/// <summary>
/// Gets the collection of Bcc recipients
/// </summary>
public MailAddressCollection Bcc
{
get
{
return Message.Bcc;
}
}
#endregion
#region delivery notification
/// <summary>
/// Gets or sets the delivery notification settings for this message
/// </summary>
public DeliveryNotificationOptions DeliveryNotifications
{
get
{
return Message.DeliveryNotificationOptions;
}
set
{
Message.DeliveryNotificationOptions = value;
}
}
#endregion
#region priority
/// <summary>
/// Gets or sets the Priority of this message
/// </summary>
public MailPriority PriorityLevel
{
get
{
return Message.Priority;
}
set
{
Message.Priority = value;
}
}
#endregion
#region send methods
/// <summary>
/// Sends the message anonymously (without credentials)
/// </summary>
public void Send()
{
SmtpMailClient.Send(Message);
}
/// <summary>
/// Sends the message with authorization from a network account
/// </summary>
/// <param name="Username">The Windows username of the authorizing user</param>
/// <param name="Password">The Windows password of the authorizing user</param>
/// <param name="Domain">The domain name of the network to which the authorizing user belongs</param>
public void Send(string Username, string Password, string Domain)
{
//attach a network credential to this message using the information passed into the method
SmtpMailClient.Credentials = new NetworkCredential(Username, Password, Domain);
//send the message
SmtpMailClient.Send(Message);
}
#endregion
#region IDisposable implementation
~SmtpMailMessage()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
if (Message != null)
Message.Dispose();
Message = null;
SmtpMailClient = null;
}
}
#endregion
}
答案 3 :(得分:0)
string BODY = String.Empty;
string PageName = "[Path to resource]"; //example C:\DataSource\Website\DomainName\RousourceFolder\page.html
BODY = new StreamReader(PageName).ReadToEnd();