发送电子邮件 w MMS 网关的附件在 gmail 中有效,但在 C# 中无效

问题描述

我的目标是向手机发送带有音频文件的彩信。我可以使用我的 gmail 帐户很好地做到这一点,只需输入我想使用正确网关域名发送消息的电话号码,(例如:123-456-7890@tmomail.net)可用于发送彩信消息到我的手机。要包含音频剪辑,我只需将其添加为附件并作为普通电子邮件发送 - 没问题。

现在,我想做同样的事情(仍然使用我的 gmail 帐户),但是使用 C# 程序发送带有附件的邮件。我制作了一个连接到我的 gmail 帐户并发送带有附件的电子邮件的功能。如果我将电子邮件发送到其他电子邮件帐户,该功能可以正常工作。但是当我把它发送到我的手机时,我只得到了邮件主题和正文,但没有附件。

这是我为完成任务而编写的函数:

using System.Net.Mail;
using System.Net;

/* NOTE: For gmail accounts with 2-Factor Authentication enabled,a special app password must be added to use to login to the account.
 * More information available here: 
 * https://support.google.com/accounts/answer/185833?hl=en
 */
 
/// <summary>
/// Sends an e-mail to the specified account(s) with the given attachment and message
/// </summary>
/// <param name="toAddresses">List of To email addresses to send message to</param>
/// <param name="message">E-mail message text</param>
/// <param name="attachedFile">Path to a file attachement to include</param>
static void SendMail(List<string> toAddresses,string subject,string message,string attachedFile = "")
{
    try
    {
        // Create new email message object
        MailMessage Message = new MailMessage();

        // Add the to address(es) to send the message to
        foreach (string address in toAddresses)
        {
            Message.To.Add(address);
        }

        // Setup the message header
        Message.Subject = subject;
        Message.From = new MailAddress("email@gmail.com");
        Message.Body = message;
        Message.IsBodyHtml = false;

        // If an attachment is provided,include it
        if (attachedFile != "")
        {
            Attachment data = new Attachment(attachedFile);
            Message.Attachments.Add(data);
        }

        // Connect to the email account and send the message
        using (SmtpClient gmailAccount = new SmtpClient("smtp.gmail.com",587))
        {
            NetworkCredential credentials = new NetworkCredential("email@gmail.com","password");
            gmailAccount.Credentials = credentials;
            gmailAccount.EnableSsl = true;
            gmailAccount.Send(Message);
        }
    }
    catch (SmtpException ex)
    {
        Console.WriteLine("Error: " + ex.Message);
    }
}

为了运行该函数,我在测试控制台应用程序的主程序中加入了以下代码:

static void Main(string[] args)
{
    Console.WriteLine("Sending Email...");

    string attachment = "C:\\File.wav";

    List<string> addresses = new List<string>();
    addresses.Add("##########@tmomail.net");

    SendMail(addresses,"Test Email","This is a test",attachment);

    Console.WriteLine("Email Program Finished.");
    Console.WriteLine();
    Console.WriteLine("Press any key to continue...");
    Console.ReadKey();
}

有没有人遇到过这样的行为?我知道电子邮件代码在我收到带有其他电子邮件帐户附件的消息时起作用。但如果我直接从我的 gmail 电子邮件中发送,它只适用于我的手机。是否缺少 gmail 本身包含但不包含我的 C# 程序的内容?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...