我写了一个C#程序来发送一封完美的电子邮件.另外,我有一个PHP脚本来发送电子邮件,它也很好用.
但我的问题是:是否可以像使用PHP一样发送带有C#的电子邮件,而不需要指定凭据,服务器,端口等.
我想使用C#而不是PHP,因为我正在创建一个ASP.Net Web应用程序.
这是我目前的C#代码:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Net.Mail; using System.Net; namespace $rootnamespace$ { public partial class $safeitemname$ : Form { public $safeitemname$() { InitializeComponent(); } private void AttachB_Click(object sender, EventArgs e) { if (AttachDia.ShowDialog() == DialogResult.OK) { string AttachF1 = AttachDia.FileName.ToString(); AttachTB.Text = AttachF1; AttachPB.Visible = true; AttachIIB.Visible = true; AttachB.Visible = false; } } private void AttachIIB_Click(object sender, EventArgs e) { if (AttachDia.ShowDialog() == DialogResult.OK) { string AttachF1 = AttachDia.FileName.ToString(); AttachIITB.Text = AttachF1; AttachPB.Visible = true; } } private void SendB_Click(object sender, EventArgs e) { try { SmtpClient client = new SmtpClient(EmailSmtpAdresTB.Text); client.EnableSsl = true; client.Timeout = 20000; client.DeliveryMethod = SmtpDeliveryMethod.Network; client.UseDefaultCredentials = false; client.Credentials = new NetworkCredential(EmailUserNameTB.Text, EmailUserPasswordTB.Text); MailMessage Msg = new MailMessage(); Msg.To.Add(SendToTB.Text); Msg.From = new MailAddress(SendFromTB.Text); Msg.Subject = SubjectTB.Text; Msg.Body = EmailTB.Text; /// Add Attachments to mail or Not if (AttachTB.Text == "") { if (EmailSmtpPortTB.Text != null) client.Port = System.Convert.ToInt32(EmailSmtpPortTB.Text); client.Send(Msg); MessageBox.Show("Successfuly Send Message !"); } else { Msg.Attachments.Add(new Attachment(AttachTB.Text)); Msg.Attachments.Add(new Attachment(AttachIITB.Text)); if (EmailSmtpPortTB.Text != null) client.Port = System.Convert.ToInt32(EmailSmtpPortTB.Text); client.Send(Msg); MessageBox.Show("Successfuly Send Message !"); } } catch (Exception ex) { MessageBox.Show(ex.Message); } } private void settingsBindingNavigatorSaveItem_Click(object sender, EventArgs e) { this.Validate(); this.settingsBindingSource.EndEdit(); this.tableAdapterManager.UpdateAll(this.awDushiHomesDBDataSet); } private void awDushiHomesEmail_Load(object sender, EventArgs e) { // TODO: This line of code loads data into the 'awDushiHomesDBDataSet.Settings' table. You can move, or remove it, as needed. this.settingsTableAdapter.Fill(this.awDushiHomesDBDataSet.Settings); } } }
这是在PHP中完成的:
--PHP-mixed- Content-Type: multipart/alternative; boundary="PHP-alt-" --PHP-alt- Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Hello World!!! This is simple text email message. --PHP-alt- Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: 7bitHello World!
This is something with HTML formatting.
--PHP-alt--- --PHP-mixed- Content-Type: application/zip; name="Doc1.pdf" Content-Transfer-Encoding: base64 Content-Disposition: attachment --PHP-mixed---
我不是要求你写我的代码,但也许你可以提供一些信息或让我知道它是否可能.
编辑:
我的问题不是不使用smtp服务器,而是如何发送电子邮件而无需输入用户名和密码.我知道,只有发送电子邮件的请求来自我的服务器才会有效.
在PHP中你可以发送邮件而不指定SMTP凭据的原因是因为其他人已经为你配置了php.ini或sendmail.ini(php解释器用来获取某些值的文件).
这通常是托管主机的情况(或者如果您使用像AMPPS这样的工具在您的开发者电脑上使用php,它可以让您通过UI轻松编辑SMTP设置并忘记它).
在ASP.net/c#中有app.config或web.config文件,您可以在其中注入smtp设置(在
标记中),因此可以获得与PHP相同的结果(SmtpClient
将自动使用存储在那里的凭据).
请参阅以下问题以获取示例:
SmtpClient和app.config system.net配置
使用配置文件的MailSettings进行SMTP身份验证